Programmatically Include Link to External Javascript File In Master Page

Discussion in 'ASP .Net' started by Joey, Jun 4, 2007.

  1. Joey

    Joey Guest

    I have the following in the head section of the ASPX page for my
    master page:

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

    This includes my external javascript code file for my master page and
    all subscribing pages. But I ran into a problem. This only works for
    pages in the root directory of the site. The (relative) path is wrong
    for pages in other folders on the website.

    I tried using src="/MyCodeFile.js", but that doesn't work. I
    understand that if I was using a server control I could use src="~/
    MyCodeFile.js".

    So what is the best way to handle this?
     
    Joey, Jun 4, 2007
    #1
    1. Advertising

  2. RE: Programmatically Include Link to External Javascript File In Maste

    You can use the RegisterClientScriptInclude method
    http://msdn2.microsoft.com/en-us/library/kx145dw2.aspx
    if necessary, along with the ResolveUrl helper method.
    Peter

    --
    Site: http://www.eggheadcafe.com
    UnBlog: http://petesbloggerama.blogspot.com
    Short urls & more: http://ittyurl.net




    "Joey" wrote:

    > I have the following in the head section of the ASPX page for my
    > master page:
    >
    > <script language="javascript" type="text/javascript"
    > src="MyCodeFile.js"></script>
    >
    > This includes my external javascript code file for my master page and
    > all subscribing pages. But I ran into a problem. This only works for
    > pages in the root directory of the site. The (relative) path is wrong
    > for pages in other folders on the website.
    >
    > I tried using src="/MyCodeFile.js", but that doesn't work. I
    > understand that if I was using a server control I could use src="~/
    > MyCodeFile.js".
    >
    > So what is the best way to handle this?
    >
    >
     
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Jun 4, 2007
    #2
    1. Advertising

  3. On Jun 4, 7:49 pm, Joey <> wrote:
    > I tried using src="/MyCodeFile.js", but that doesn't work.


    What the path of your web application is?

    If something like http://localhost/MyApp/ then you should use src="/
    MyApp/MyCodeFile.js"
     
    Alexey Smirnov, Jun 4, 2007
    #3
  4. Joey

    Joey Guest

    Re: Programmatically Include Link to External Javascript File In Maste

    On Jun 4, 1:31 pm, Peter Bromberg [C# MVP]
    <> wrote:
    > You can use the RegisterClientScriptInclude methodhttp://msdn2.microsoft.com/en-us/library/kx145dw2.aspx
    > if necessary, along with the ResolveUrl helper method.
    > Peter
    >
    > --
    > Site: http://www.eggheadcafe.com
    > UnBlog: http://petesbloggerama.blogspot.com
    > Short urls & more: http://ittyurl.net
    >
    >
    >
    > "Joey" wrote:
    > > I have the following in the head section of the ASPX page for my
    > > master page:

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

    >
    > > This includes my external javascript code file for my master page and
    > > all subscribing pages. But I ran into a problem. This only works for
    > > pages in the root directory of the site. The (relative) path is wrong
    > > for pages in other folders on the website.

    >
    > > I tried using src="/MyCodeFile.js", but that doesn't work. I
    > > understand that if I was using a server control I could use src="~/
    > > MyCodeFile.js".

    >
    > > So what is the best way to handle this?- Hide quoted text -

    >
    > - Show quoted text -


    Could you provide an example for how to do this?

    I am familiar with ClientScript.RegisterStartupScript(), etc...I use
    it to register scripts located in my external javascript file. But how
    do I register the file itself?
     
    Joey, Jun 4, 2007
    #4
  5. Re: Programmatically Include Link to External Javascript File In M

    Joey,
    You need to go back and read the MSDN page at the link I provided in my
    previous response. If you still "don't get it, post again and somebody will
    respond.
    Peter

    --
    Site: http://www.eggheadcafe.com
    UnBlog: http://petesbloggerama.blogspot.com
    Short urls & more: http://ittyurl.net




    "Joey" wrote:

    > On Jun 4, 1:31 pm, Peter Bromberg [C# MVP]
    > <> wrote:
    > > You can use the RegisterClientScriptInclude methodhttp://msdn2.microsoft.com/en-us/library/kx145dw2.aspx
    > > if necessary, along with the ResolveUrl helper method.
    > > Peter
    > >
    > > --
    > > Site: http://www.eggheadcafe.com
    > > UnBlog: http://petesbloggerama.blogspot.com
    > > Short urls & more: http://ittyurl.net
    > >
    > >
    > >
    > > "Joey" wrote:
    > > > I have the following in the head section of the ASPX page for my
    > > > master page:

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

    > >
    > > > This includes my external javascript code file for my master page and
    > > > all subscribing pages. But I ran into a problem. This only works for
    > > > pages in the root directory of the site. The (relative) path is wrong
    > > > for pages in other folders on the website.

    > >
    > > > I tried using src="/MyCodeFile.js", but that doesn't work. I
    > > > understand that if I was using a server control I could use src="~/
    > > > MyCodeFile.js".

    > >
    > > > So what is the best way to handle this?- Hide quoted text -

    > >
    > > - Show quoted text -

    >
    > Could you provide an example for how to do this?
    >
    > I am familiar with ClientScript.RegisterStartupScript(), etc...I use
    > it to register scripts located in my external javascript file. But how
    > do I register the file itself?
    >
    >
     
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Jun 4, 2007
    #5
  6. Joey

    Joey Guest

    Re: Programmatically Include Link to External Javascript File In M

    On Jun 4, 3:39 pm, Peter Bromberg [C# MVP]
    <> wrote:
    > Joey,
    > You need to go back and read the MSDN page at the link I provided in my
    > previous response. If you still "don't get it, post again and somebody will
    > respond.
    > Peter
    >
    > --
    > Site: http://www.eggheadcafe.com
    > UnBlog: http://petesbloggerama.blogspot.com
    > Short urls & more: http://ittyurl.net
    >
    >
    >
    > "Joey" wrote:
    > > On Jun 4, 1:31 pm, Peter Bromberg [C# MVP]
    > > <> wrote:
    > > > You can use the RegisterClientScriptInclude methodhttp://msdn2.microsoft.com/en-us/library/kx145dw2.aspx
    > > > if necessary, along with the ResolveUrl helper method.
    > > > Peter

    >
    > > > --
    > > > Site: http://www.eggheadcafe.com
    > > > UnBlog: http://petesbloggerama.blogspot.com
    > > > Short urls & more: http://ittyurl.net

    >
    > > > "Joey" wrote:
    > > > > I have the following in the head section of the ASPX page for my
    > > > > master page:

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

    >
    > > > > This includes my externaljavascriptcodefilefor my master page and
    > > > > all subscribing pages. But I ran into a problem. This only works for
    > > > > pages in the root directory of the site. The (relative) path is wrong
    > > > > for pages in other folders on the website.

    >
    > > > > I tried using src="/MyCodeFile.js", but that doesn't work. I
    > > > > understand that if I was using a server control I could use src="~/
    > > > > MyCodeFile.js".

    >
    > > > > So what is the best way to handle this?- Hide quoted text -

    >
    > > > - Show quoted text -

    >
    > > Could you provide an example for how to do this?

    >
    > > I am familiar with ClientScript.RegisterStartupScript(), etc...I use
    > > it to register scripts located in my externaljavascriptfile. But how
    > > do I register thefileitself?- Hide quoted text -

    >
    > - Show quoted text -


    I got it. Thanks for the link.

    JP
     
    Joey, Jun 18, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Doug McCann
    Replies:
    1
    Views:
    1,030
    William Brogden
    Aug 5, 2004
  2. Zeba
    Replies:
    1
    Views:
    496
    Mark Rae
    Feb 22, 2007
  3. Andreas Bogenberger
    Replies:
    3
    Views:
    927
    Andreas Bogenberger
    Feb 22, 2008
  4. Nathan Sokalski
    Replies:
    25
    Views:
    1,438
    Nathan Sokalski
    Mar 4, 2010
  5. SerpentKiss2010
    Replies:
    0
    Views:
    2,494
    SerpentKiss2010
    Apr 6, 2011
Loading...

Share This Page