Specifying javascript file path on Master pages

Discussion in 'ASP .Net' started by Naveen K Kohli, Feb 5, 2006.

  1. I have couple of javascript files that need to be referenced for each
    content page. So I am trying to put them on Master page. The problem is that
    content pages are not on one level of the site. I have been trying to find a
    way to specify path to these JS files so that the actual path to these files
    correctly gets applied on the path at every level of the folder hierarchies.
    So far no luck...

    <script languaage=javascript src="/Scripts/myfile.js" />

    Is there any solution to this problem? I have tried to use Server.MapPath
    approach to fill src attribute from server side but that does not fly
    because it throws exception that controls can't be added or removed on the
    page because of theese server tags present on the page..

    Any solution to this problem.. I am this close to abandoning use of master
    page concept of asp.net 2

    Thanks
     
    Naveen K Kohli, Feb 5, 2006
    #1
    1. Advertising

  2. Hi Naveen,

    The tilde character (~) stands for the site's root when ASP.NET interprets
    it. Therefore, you can create a link to your JavaScript that runs on the
    server. I

    <link runat="server" href="~/scripts/myfile.js" />

    With that, every file should find it.

    Let us know if this helps?

    Ken
    Microsoft MVP [ASP.NET]


    "Naveen K Kohli" <> wrote in message
    news:...
    >I have couple of javascript files that need to be referenced for each
    >content page. So I am trying to put them on Master page. The problem is
    >that content pages are not on one level of the site. I have been trying to
    >find a way to specify path to these JS files so that the actual path to
    >these files correctly gets applied on the path at every level of the folder
    >hierarchies. So far no luck...
    >
    > <script languaage=javascript src="/Scripts/myfile.js" />
    >
    > Is there any solution to this problem? I have tried to use Server.MapPath
    > approach to fill src attribute from server side but that does not fly
    > because it throws exception that controls can't be added or removed on the
    > page because of theese server tags present on the page..
    >
    > Any solution to this problem.. I am this close to abandoning use of master
    > page concept of asp.net 2
    >
    > Thanks
    >
     
    Ken Cox - Microsoft MVP, Feb 5, 2006
    #2
    1. Advertising

  3. Already tried that approach. It does not work at all.

    "Ken Cox - Microsoft MVP" <> wrote in message
    news:%...
    > Hi Naveen,
    >
    > The tilde character (~) stands for the site's root when ASP.NET interprets
    > it. Therefore, you can create a link to your JavaScript that runs on the
    > server. I
    >
    > <link runat="server" href="~/scripts/myfile.js" />
    >
    > With that, every file should find it.
    >
    > Let us know if this helps?
    >
    > Ken
    > Microsoft MVP [ASP.NET]
    >
    >
    > "Naveen K Kohli" <> wrote in message
    > news:...
    >>I have couple of javascript files that need to be referenced for each
    >>content page. So I am trying to put them on Master page. The problem is
    >>that content pages are not on one level of the site. I have been trying to
    >>find a way to specify path to these JS files so that the actual path to
    >>these files correctly gets applied on the path at every level of the
    >>folder hierarchies. So far no luck...
    >>
    >> <script languaage=javascript src="/Scripts/myfile.js" />
    >>
    >> Is there any solution to this problem? I have tried to use Server.MapPath
    >> approach to fill src attribute from server side but that does not fly
    >> because it throws exception that controls can't be added or removed on
    >> the page because of theese server tags present on the page..
    >>
    >> Any solution to this problem.. I am this close to abandoning use of
    >> master page concept of asp.net 2
    >>
    >> Thanks
    >>

    >
    >
     
    Naveen K Kohli, Feb 5, 2006
    #3
  4. Show us the code that doesn't work?

    "Naveen K Kohli" <> wrote in message
    news:Og$...
    > Already tried that approach. It does not work at all.
    >
    > "Ken Cox - Microsoft MVP" <> wrote in message
    > news:%...
    >> Hi Naveen,
    >>
    >> The tilde character (~) stands for the site's root when ASP.NET
    >> interprets it. Therefore, you can create a link to your JavaScript that
    >> runs on the server. I
    >>
    >> <link runat="server" href="~/scripts/myfile.js" />
    >>
    >> With that, every file should find it.
    >>
    >> Let us know if this helps?
    >>
    >> Ken
    >> Microsoft MVP [ASP.NET]
    >>
    >>
    >> "Naveen K Kohli" <> wrote in message
    >> news:...
    >>>I have couple of javascript files that need to be referenced for each
    >>>content page. So I am trying to put them on Master page. The problem is
    >>>that content pages are not on one level of the site. I have been trying
    >>>to find a way to specify path to these JS files so that the actual path
    >>>to these files correctly gets applied on the path at every level of the
    >>>folder hierarchies. So far no luck...
    >>>
    >>> <script languaage=javascript src="/Scripts/myfile.js" />
    >>>
    >>> Is there any solution to this problem? I have tried to use
    >>> Server.MapPath approach to fill src attribute from server side but that
    >>> does not fly because it throws exception that controls can't be added or
    >>> removed on the page because of theese server tags present on the page..
    >>>
    >>> Any solution to this problem.. I am this close to abandoning use of
    >>> master page concept of asp.net 2
    >>>
    >>> Thanks
    >>>

    >>
    >>

    >
    >
     
    Ken Cox - Microsoft MVP, Feb 5, 2006
    #4
  5. There is just one like of code... ShowAlert() function in the following code
    is defined in my MyDOM.js file. And if you look at the loaded documents
    (pages, js etc.) in browser, MyDOM.js is not there. And the call to
    ShowAlert ends us generting a JS error.

    <head runat=server>
    <link href="~/Scripts/MyDOM.js" />
    </head>

    <body>
    <script language=javascript>
    ShowAlert();
    </script>
    </body>

    "Ken Cox - Microsoft MVP" <> wrote in message
    news:%...
    > Show us the code that doesn't work?
    >
    > "Naveen K Kohli" <> wrote in message
    > news:Og$...
    >> Already tried that approach. It does not work at all.
    >>
    >> "Ken Cox - Microsoft MVP" <> wrote in message
    >> news:%...
    >>> Hi Naveen,
    >>>
    >>> The tilde character (~) stands for the site's root when ASP.NET
    >>> interprets it. Therefore, you can create a link to your JavaScript that
    >>> runs on the server. I
    >>>
    >>> <link runat="server" href="~/scripts/myfile.js" />
    >>>
    >>> With that, every file should find it.
    >>>
    >>> Let us know if this helps?
    >>>
    >>> Ken
    >>> Microsoft MVP [ASP.NET]
    >>>
    >>>
    >>> "Naveen K Kohli" <> wrote in message
    >>> news:...
    >>>>I have couple of javascript files that need to be referenced for each
    >>>>content page. So I am trying to put them on Master page. The problem is
    >>>>that content pages are not on one level of the site. I have been trying
    >>>>to find a way to specify path to these JS files so that the actual path
    >>>>to these files correctly gets applied on the path at every level of the
    >>>>folder hierarchies. So far no luck...
    >>>>
    >>>> <script languaage=javascript src="/Scripts/myfile.js" />
    >>>>
    >>>> Is there any solution to this problem? I have tried to use
    >>>> Server.MapPath approach to fill src attribute from server side but that
    >>>> does not fly because it throws exception that controls can't be added
    >>>> or removed on the page because of theese server tags present on the
    >>>> page..
    >>>>
    >>>> Any solution to this problem.. I am this close to abandoning use of
    >>>> master page concept of asp.net 2
    >>>>
    >>>> Thanks
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
     
    Naveen K Kohli, Feb 5, 2006
    #5
  6. You didn't follow my example properly. You must include runat="server".

    Here it is again:

    <link runat="server" href="~/scripts/myfile.js" />

    Ken

    "Naveen K Kohli" <> wrote in message
    news:...
    > There is just one like of code... ShowAlert() function in the following
    > code is defined in my MyDOM.js file. And if you look at the loaded
    > documents (pages, js etc.) in browser, MyDOM.js is not there. And the call
    > to ShowAlert ends us generting a JS error.
    >
    > <head runat=server>
    > <link href="~/Scripts/MyDOM.js" />
    > </head>
    >
    > <body>
    > <script language=javascript>
    > ShowAlert();
    > </script>
    > </body>
    >
    > "Ken Cox - Microsoft MVP" <> wrote in message
    > news:%...
    >> Show us the code that doesn't work?
    >>
    >> "Naveen K Kohli" <> wrote in message
    >> news:Og$...
    >>> Already tried that approach. It does not work at all.
    >>>
    >>> "Ken Cox - Microsoft MVP" <> wrote in message
    >>> news:%...
    >>>> Hi Naveen,
    >>>>
    >>>> The tilde character (~) stands for the site's root when ASP.NET
    >>>> interprets it. Therefore, you can create a link to your JavaScript that
    >>>> runs on the server. I
    >>>>
    >>>> <link runat="server" href="~/scripts/myfile.js" />
    >>>>
    >>>> With that, every file should find it.
    >>>>
    >>>> Let us know if this helps?
    >>>>
    >>>> Ken
    >>>> Microsoft MVP [ASP.NET]
    >>>>
    >>>>
    >>>> "Naveen K Kohli" <> wrote in message
    >>>> news:...
    >>>>>I have couple of javascript files that need to be referenced for each
    >>>>>content page. So I am trying to put them on Master page. The problem is
    >>>>>that content pages are not on one level of the site. I have been trying
    >>>>>to find a way to specify path to these JS files so that the actual path
    >>>>>to these files correctly gets applied on the path at every level of the
    >>>>>folder hierarchies. So far no luck...
    >>>>>
    >>>>> <script languaage=javascript src="/Scripts/myfile.js" />
    >>>>>
    >>>>> Is there any solution to this problem? I have tried to use
    >>>>> Server.MapPath approach to fill src attribute from server side but
    >>>>> that does not fly because it throws exception that controls can't be
    >>>>> added or removed on the page because of theese server tags present on
    >>>>> the page..
    >>>>>
    >>>>> Any solution to this problem.. I am this close to abandoning use of
    >>>>> master page concept of asp.net 2
    >>>>>
    >>>>> Thanks
    >>>>>
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
     
    Ken Cox - Microsoft MVP, Feb 5, 2006
    #6
  7. My bad.. I did follow what you said. While typing the code in the post I
    forgot to write it.

    "Ken Cox - Microsoft MVP" <> wrote in message
    news:...
    > You didn't follow my example properly. You must include runat="server".
    >
    > Here it is again:
    >
    > <link runat="server" href="~/scripts/myfile.js" />
    >
    > Ken
    >
    > "Naveen K Kohli" <> wrote in message
    > news:...
    >> There is just one like of code... ShowAlert() function in the following
    >> code is defined in my MyDOM.js file. And if you look at the loaded
    >> documents (pages, js etc.) in browser, MyDOM.js is not there. And the
    >> call to ShowAlert ends us generting a JS error.
    >>
    >> <head runat=server>
    >> <link href="~/Scripts/MyDOM.js" />
    >> </head>
    >>
    >> <body>
    >> <script language=javascript>
    >> ShowAlert();
    >> </script>
    >> </body>
    >>
    >> "Ken Cox - Microsoft MVP" <> wrote in message
    >> news:%...
    >>> Show us the code that doesn't work?
    >>>
    >>> "Naveen K Kohli" <> wrote in message
    >>> news:Og$...
    >>>> Already tried that approach. It does not work at all.
    >>>>
    >>>> "Ken Cox - Microsoft MVP" <> wrote in message
    >>>> news:%...
    >>>>> Hi Naveen,
    >>>>>
    >>>>> The tilde character (~) stands for the site's root when ASP.NET
    >>>>> interprets it. Therefore, you can create a link to your JavaScript
    >>>>> that runs on the server. I
    >>>>>
    >>>>> <link runat="server" href="~/scripts/myfile.js" />
    >>>>>
    >>>>> With that, every file should find it.
    >>>>>
    >>>>> Let us know if this helps?
    >>>>>
    >>>>> Ken
    >>>>> Microsoft MVP [ASP.NET]
    >>>>>
    >>>>>
    >>>>> "Naveen K Kohli" <> wrote in message
    >>>>> news:...
    >>>>>>I have couple of javascript files that need to be referenced for each
    >>>>>>content page. So I am trying to put them on Master page. The problem
    >>>>>>is that content pages are not on one level of the site. I have been
    >>>>>>trying to find a way to specify path to these JS files so that the
    >>>>>>actual path to these files correctly gets applied on the path at every
    >>>>>>level of the folder hierarchies. So far no luck...
    >>>>>>
    >>>>>> <script languaage=javascript src="/Scripts/myfile.js" />
    >>>>>>
    >>>>>> Is there any solution to this problem? I have tried to use
    >>>>>> Server.MapPath approach to fill src attribute from server side but
    >>>>>> that does not fly because it throws exception that controls can't be
    >>>>>> added or removed on the page because of theese server tags present on
    >>>>>> the page..
    >>>>>>
    >>>>>> Any solution to this problem.. I am this close to abandoning use of
    >>>>>> master page concept of asp.net 2
    >>>>>>
    >>>>>> Thanks
    >>>>>>
    >>>>>
    >>>>>
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
     
    Naveen K Kohli, Feb 6, 2006
    #7
    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. Yang Xiao
    Replies:
    4
    Views:
    38,764
    czahrobsky
    May 30, 2010
  2. Doug McCann
    Replies:
    1
    Views:
    1,079
    William Brogden
    Aug 5, 2004
  3. Replies:
    1
    Views:
    3,250
    PeterKellner
    May 16, 2006
  4. Greg Collins [Microsoft MVP]

    Identifying the file that is specifying the master

    Greg Collins [Microsoft MVP], Jan 2, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    291
    Greg Collins [Microsoft MVP]
    Jan 2, 2007
  5. Replies:
    6
    Views:
    117
Loading...

Share This Page