Dynamically including JavaScript files in MasterPages

Last post 02-05-2008 6:22 AM by ad_dc. 5 replies.

Sort Posts:

  • Dynamically including JavaScript files in MasterPages

    08-04-2005, 6:02 PM
    • Loading...
    • BurntSky
    • Joined on 05-04-2004, 11:20 AM
    • Bellevue, WA
    • Posts 156
    I have a MasterPage that is used by pages spread out through a number of directories.  e.g.:

    ~/MasterPage.master
    is used by:
    ~/Pages/SomePage.aspx
    ~/Pages/SomeDirectory/SomePage2.aspx
    ~/Pages/AnotherDirectory/AnotherPage.aspx

    However, all the pages require access to a global javascript file (e.g. ~/Scripts/Default.js).  If I attach the javascript file to the MasterPage with an "src" attribute relative to the MasterPage, none of the other pages will load it.  I don't want to have to do the following:

    <head>
       <script type="text/javascript" language="javascript" src="../Scripts/Default.js"></script>
       <script type="text/javascript" language="javascript" src="../../Scripts/Default.js"></script>
    </head>

    Is there a way to dynamically attach the javascript file with the correct relative URL?
  • Re: Dynamically including JavaScript files in MasterPages

    08-04-2005, 11:24 PM
    • Loading...
    • adam11235
    • Joined on 05-23-2005, 12:09 AM
    • Posts 23
    Hi,

    In the Page Load event of your MasterPage:

    this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "Global", this.ResolveClientUrl("~/javascript/Global.js"));

    Cheers,

    Adam.

  • Re: Dynamically including JavaScript files in MasterPages

    08-05-2005, 3:33 AM
    • Loading...
    • BurntSky
    • Joined on 05-04-2004, 11:20 AM
    • Bellevue, WA
    • Posts 156
    Ohhh man, that's a pretty neat trick.  Thanks.
  • Re: Dynamically including JavaScript files in MasterPages

    02-22-2006, 4:38 AM
    • Loading...
    • seamoose
    • Joined on 02-22-2006, 9:35 AM
    • Posts 9

    adam11235 wrote:
    Hi,

    In the Page Load event of your MasterPage:

    Hi

    My masterpage doesn't seem to have a Page Load event   (in fact, there is no code behind at all) - how do I get to it?

    Thanks for any help!

    Jase

  • Betreft: Re: Dynamically including JavaScript files in MasterPages

    03-01-2006, 3:25 AM
    • Loading...
    • webbes
    • Joined on 07-24-2002, 8:27 AM
    • Netherlands
    • Posts 419

    Having a thought....

    Doesn't this work?(I don't have the opportunity to test it out over here...)

    <script type="text/javascript" src="~/Scripts/Default.js" runat="server"></script>

    Cheers,

    Wes

    Listen carefully, I will say this only once...
  • Re: Dynamically including JavaScript files in MasterPages

    02-05-2008, 6:22 AM
    • ad_dc
    • Joined on 10-04-2007, 6:15 AM
    • Posts 64

     Really you are a big prince Wink, it's very good work man

    but let me know what  "Global" (key) means ??? and if I put any name nothing affected ??

    thank you very much  again and again.

Page 1 of 1 (6 items)