Including a javascript file in a MasterPage

Last post 05-21-2009 3:12 AM by prashanthganathe. 12 replies.

Sort Posts:

  • Including a javascript file in a MasterPage

    09-05-2006, 11:29 AM
    • Member
      191 point Member
    • rturner003
    • Member since 07-31-2003, 2:13 AM
    • Cheltenham, UK
    • Posts 41

    I am trying to include a Javascript file in MasterPage but although it shows up when you look at the page source of the resultant web page the controls that reference functions within (on mouseover etc) cannot find the functions.

    I first used the standard <script> code in the masterpage but found that as I was using the asp:menu control that would play up if I put it directly in the head area (red herring).

    I then tried this piece of post in the page behind file:

    protected void Page_Load(object sender, EventArgs e)

    {

    HtmlGenericControl si = new HtmlGenericControl();

    si.TagName = "script";

    si.Attributes.Add("type", "text/javascript");

    si.Attributes.Add("language", "javascript");

    si.Attributes.Add("src", "Scripts/tooltips.js");

    this.Page.Header.Controls.Add(si);

    }

    Can anyone please tell me where I am going wrong or what I need to do

  • Re: Including a javascript file in a MasterPage

    09-05-2006, 10:08 PM
    • Participant
      1,124 point Participant
    • MuteThis
    • Member since 09-21-2003, 1:10 PM
    • Cincinnati, Ohio
    • Posts 208

    Since you are using a MasterPages if you always want the file to be included you can add a reference to it declaratively in the <head> tags using <script type="text/javascript" etc />.  If you want to add it programmatically what you have looks good, I'm curious if you're having pathing issues...  Scripts/tooltips.js is a relative path and will not work properly from subfolders.  Perhaps you want /Scripts/tooltips.js, or maybe you should be building it using the application path.

    Ben
    MCAD .Net (70-315, 70-320 & 70-229)
    :One that throws dirt loses ground:
  • Re: Including a javascript file in a MasterPage

    09-05-2006, 10:27 PM
    • Contributor
      3,041 point Contributor
    • lostlander
    • Member since 05-22-2006, 11:55 AM
    • Posts 607

    Try below:

    <script runat="server">
       private void Page_Load(object sender, System.EventArgs e) {
          this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "Global", this.ResolveClientUrl "~/Scripts/tooltips.js"));
       }
    </script>
    For more information:
    http://forums.asp.net/thread/1276008.aspx
    and :
    http://forums.asp.net/search/ with keyword: "javascript masterpage".
    Hope it helps!
    Regards!^_^
    Lostlander.
     
  • Re: Including a javascript file in a MasterPage

    09-06-2006, 1:58 AM
    • Member
      191 point Member
    • rturner003
    • Member since 07-31-2003, 2:13 AM
    • Cheltenham, UK
    • Posts 41

    This works as long as the Javascript file is in the same folder as the aspx files.

    this works:

    string strJSFile;

    strJSFile = this.ResolveClientUrl("tooltips.js");

    Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "mykey", strJSFile);

    but this doesn't

    string strJSFile;

    strJSFile = this.ResolveClientUrl("~/Scripts/tooltips.js");

    Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "mykey", strJSFile);

    this places the script tag as src="Scripts/tooltips.js"

    So although I have it working and in a .js file I would prefer all the .js files in there own folder.

    Thanks

  • Re: Including a javascript file in a MasterPage

    09-06-2006, 2:16 AM
    • Contributor
      3,041 point Contributor
    • lostlander
    • Member since 05-22-2006, 11:55 AM
    • Posts 607

    the symbol ~ seems not to be translated, try using two dots like : ../Scripts/tooltip.js

    Regads!^_^
    Lostlander.

  • Re: Including a javascript file in a MasterPage

    09-06-2006, 4:26 AM
    • Member
      191 point Member
    • rturner003
    • Member since 07-31-2003, 2:13 AM
    • Cheltenham, UK
    • Posts 41

    Although that made a difference in the results code:

    <script src="../Scripts/tooltips.js" type="text/javascript"></script>

    it still did not work.

  • Re: Including a javascript file in a MasterPage

    09-06-2006, 4:46 AM
    Answer
    • Contributor
      3,041 point Contributor
    • lostlander
    • Member since 05-22-2006, 11:55 AM
    • Posts 607

    .. Means the aspx file's parent folder.

    Make sure the aspx file is located like this:

    +root
        +folder
            +temp.aspx
        +Scripts
            tootip.js

    When you browse temp.aspx, So the path "../Scripts/tooltips.js" would be resolved to be : root\Scripts\tooltip.js.
    Hope it helps!^_^
        

  • Re: Including a javascript file in a MasterPage

    09-06-2006, 5:48 AM
    • Member
      191 point Member
    • rturner003
    • Member since 07-31-2003, 2:13 AM
    • Cheltenham, UK
    • Posts 41

    my folder layout is suc:

    c:\LocalHost is the root folder of my web system

    c:\localhost\taskmanager is the root folder of my application and where my .aspx files are

    c:\localhost\taskmanger\scripts is where I want to store my script files

    I also have the following folders under c:\localhost\taskmanager :

           styles

           images

           App_Code

    I have had no problem with styles and images

        

     

     

     

  • Re: Including a javascript file in a MasterPage

    05-02-2007, 7:09 AM
    • Member
      39 point Member
    • S!ava
    • Member since 04-11-2007, 9:36 AM
    • Moldova
    • Posts 18
    lostlander:

    Try below:

    <script runat="server">
    private void Page_Load(object sender, System.EventArgs e) {
    this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "Global", this.ResolveClientUrl "~/Scripts/tooltips.js"));
    }
    </script>
    For more information:
    http://forums.asp.net/thread/1276008.aspx
    and :
    http://forums.asp.net/search/ with keyword: "javascript masterpage".
    Hope it helps!
    Regards!^_^
    Lostlander.

     

     
    Thanks, it worked! I just had to know the function witch translates the relative URL to an Absolute. For me - I used it in asp page in the body tag, it's easier.
     

     <script language="javascript" src='<%= this.ResolveClientUrl("~/js/ddnmenu.js") %>' type="text/javascript"></script>
     
    Thanks again Yes
  • Re: Including a javascript file in a MasterPage

    05-09-2007, 9:59 AM
    • Member
      21 point Member
    • RNUK
    • Member since 01-11-2007, 2:24 PM
    • Posts 53

    Hi.

    I have used the above solution to define a JS file from a master page. I have a master page in the top level of my site and all the ASPX files are in sub folders defined by their section ie:

    Root Dir
         Section 1
         Section 2
         Section 3

    so on a so forth.

    This worked a charm up until the latest toolkit release! I downloaded the toolkit as I am using tabs in an update panel (The script error has been rectified in this release), however, this has knocked out the ResolveClient URL tag. Now I get an exception (But on ly in the head tag) when I am registering a JS file, of:

    The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

    This is in the:

    public static void RegisterCssReferences(Control control)

    {

    foreach (string styleSheet in ScriptObjectBuilder.GetCssReferences(control))

    {

    // Add the link to the page header instead of inside the body which is not xhtml compliant

    HtmlLink link =

    new HtmlLink();

    link.Href = styleSheet;

    link.Attributes.Add(

    "type", "text/css");

    link.Attributes.Add(

    "rel", "stylesheet");

    control.Page.Header.Controls.Add(link);

    }

    }

    section of ScriptObjectBuilder.CS

    Has anyone else come up against this?
     

  • Re: Including a javascript file in a MasterPage

    05-09-2007, 10:11 AM
    • Member
      21 point Member
    • RNUK
    • Member since 01-11-2007, 2:24 PM
    • Posts 53

    Sorry, I have resolved this:

    Note: You can no longer just put this in the tags in the header, it has to be done in code behind.

    VB version of resolution above...

    Page.ClientScript.RegisterClientScriptInclude(Page.GetType(),

    "Global", ResolveClientUrl("JS/navlist.js"))

     

  • Re: Including a javascript file in a MasterPage

    02-10-2009, 5:00 AM
    • Member
      103 point Member
    • babuji_godem
    • Member since 11-20-2007, 11:59 AM
    • Hyderabad
    • Posts 51

     you may use full url like

     <script type="text/javascript" language="javascript" src="http://localhost/Examples/Includes/Main.js">

    or

    you may set base url in head section

    <base href="http://localhost/Examples/"></base>

     <script type="text/javascript" language="javascript" src="Includes/Main.js"> 

     

     

    Babuji Godem
    Human Logic Pvt Ltd.
  • Re: Including a javascript file in a MasterPage

    05-21-2009, 3:12 AM
    I found the best and simple solution it is just adding "ResolveUrl" while referencing the javascript file. link : http://aspnetnova.blogspot.com/2009/05/masterpage-contentpage-javascript-path.html
Page 1 of 1 (13 items)
Microsoft Communities