Link in Javascript Source Documents

  • Thread starter Thread starter Joey
  • Start date Start date
J

Joey

Can someone provide an example of how to link in a javascript source
file into the master page of my asp.net 2.0 web application? In asp.net
1.1 I always used the following line of code in each individual page...

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

However, when I try to use it in a master page in asp.net 2.0, the src
attribute does not map
properly.

src="MyScripts.js" doesn't work, unless you're on a page in the site
root folder.

src="~/MyScripts.js" doesn't work, either.

src="/MyScripts.js" works as long as you deploy and hit
"http:\\localhost" from IIS.

But I am trying to run in file system mode. I have also seen this
mapping problem with some HTML controls that I using (images, etc...).
One cannot and should not always use a server control.

What should I do?
 
Use a root-relative URL. If the file is in the root folder, for example,
put:

src="/MyScripts.js"

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.
 
Thanks, Kevin, but I had that in my original post. The problem when
doing it that way is...file system websites are launched like virtual
(sub) directories. For example, the URL would be...

http://localhost:4892/mysite <- "/MyFile.js" does not map to site root

instead of

http://localhost <- "/MyFile.js" maps fine

as in IIS.

Therefore it doesn't work in file system mode.
 
Hi Joey,

One solution is to add a root path the the web.config file, which is
concatenated with relative paths in the app to produce fully-qualified URLs
on the site. The root path can be changed when you publish.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.
 
Back
Top