+1-888-365-2779
Try Now
More in this section

Forums / Designing with Sitefinity / Linking to Javascript files in Master Pages

Linking to Javascript files in Master Pages

7 posts, 0 answered
  1. JP D
    JP D avatar
    12 posts
    Registered:
    16 Feb 2010
    23 Feb 2010
    Link to this post
    Hi.

    I have some javascript effects on all of my pages.  Everything works great in the root directory, but the links to the javascript files break in the subdirectories.  What is the best way to include the javascript files in the master pages so that they can be found even in the subdirectories?

    It is curious to me because the link to the stylesheet never breaks, even in the subdirectories.  In fact, the url is automatically prepended with  ../App_Master/.  Is it possible to achieve this with the links to the Javascript files as well?

    Thanks so much,

    JP
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    19 Sep 2016
    24 Feb 2010
    Link to this post
    Hi JP D,

    Please take a look at Adding Javascript file link in Master Page

    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. JP D
    JP D avatar
    12 posts
    Registered:
    16 Feb 2010
    24 Feb 2010
    Link to this post
    Thanks Ivan, that will work.  Can you explain why I don't have to do this with the link to the css stylesheet?
  4. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    24 Feb 2010
    Link to this post
    try making sure your src is relative to the root

    that is instead of say

    script src="scripts/myscript.js"

    put

    script = src"/scripts/myscript.js"

    that is how I reference scripts on my page and it works fine. relative links will be relative to the PATH of the url, so if your page is in a subfolder (even a virtual one, like a page group) it's going to retrieve from a location relative to that point.

    hope this was helpful!
  5. JP D
    JP D avatar
    12 posts
    Registered:
    16 Feb 2010
    24 Feb 2010
    Link to this post
    Thanks SelArom,

    Your solution would work, but I want to avoid having to create new master pages for each subfolder (each with the correct relative filepath to the .js file). 

    The code that Ivan linked to works great, and allows me to use a single master page that maintains its link to the javascript no matter how deep in the directory the aspx page is.
  6. Nancy
    Nancy avatar
    35 posts
    Registered:
    10 Apr 2012
    09 May 2012
    Link to this post
    This is not working for me because the Sitefinity project manager appends the project name to the URL so /Scripts/libs/modernizr-2.5.3.min.js is actually not found. It can find it if I use /Projectname/Scripts/libs/modernizr-2.5.3.min.js, but then it won't work when I deploy to the server where there is no project name in the path. D'oh! 
  7. Nancy
    Nancy avatar
    35 posts
    Registered:
    10 Apr 2012
    09 May 2012
    Link to this post
    After that last post, I found this article.  http://www.mayvelous.com/2011/07/22/sitefinity-linking-external-js-file-in-master-pages/  That solution is working well for me. 

    I added this line to the top of my masterpage, right after the @ Master line. 
    <%@ Register TagPrefix="sf" Assembly="Telerik.Sitefinity" Namespace="Telerik.Sitefinity.Web.UI.PublicControls"%>

    Then added the following inside the form tag of my page...even though I want the script added in the head tag. The ScriptEmbedPosition="Head" puts it in the right place. 
    <sf:JavaScriptEmbedControl runat="server" ID="jsLink1" ScriptEmbedPosition="Head" Url="~/Scripts/libs/modernizr-2.5.3.min.js"></sf:JavaScriptEmbedControl>

Register for webinar
7 posts, 0 answered