Problems with .js files using Master Pages

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, all:

I have a problem with the inclusion of .js files in mu .aspx pages when
using Master Pages. I try to explain it. If I make a web project without
master pages I simply put in the head tag on each page the reference to the
..js file I want to use. Because I want client side includes I make the
reference with <script> tag, for example:
<head runat=server>
<script src="/javascript/test.js" />
</head>

But, If I try to make the same project using Master Pages this don´t work. I
suppose I have to put the references to .js files in the master pages. So I
put the code listed before in the master page <head> tag, create an .aspx
page, use a function of the .js file and it doesn´t work. But, If I put this
path ( "javascript/test.js") instead of this ( "/javascript/test.js") it
works fine (note the difference in the first backslash). I need to use the
first one, with the first backslash. Anyone knows which is the problem?

Any help would be appreciated.
 
/javascript/test.js should work OK, what is the difference in the HTML output
between the two versions? If you HTML is getting rendered exactly as you
wanted, what happens if you put srcript tags ina standard HTML page on the
server, does it do the same thing? If so perhaps it's a server configuration
issue rather than an ASP.net thing.
 
Hi, clickon:

the html rendered is exactly the same except for the first backslash. If i
use "javascript/test.js" or "./javascript/test.js" it works fine. With
"/javascript/test.js" it doesn´t works. And this only happens with master
pages. I´m confused... :)
 
Have you tried <script src="~/javascript/test.js" /> ?

The tilde will map to the root of the virtual directory if your head is
running at server.
 
Well, the problem is that I need the path begins with only a backslash. Why?
Because in production the application static content (.css, .js, .gif, ...)
will be under Apache, and this is the only way for reference them. For
example, we will have a virtual directory in Apche for all images called
"IMAGES" and we have to reference it from the code as "/IMAGES/image_name".
Do uoy understand what I mean?

Thanks a lot for your interest!
 
I do understand, the initial forward slash (backslash is \) refers to
the root of the application - if your head is running at the server and
you refer to your files with ~/SomeDir the output should, in your case,
just be /SomeDir. Or am I missing something?
 
Definatley don't put a tilde in a url if you are using apache as you may get
vewry strange results. A tilde in apaches on *nix refers to a users home
directory e.g. http://www.somedomeian.com/~FredB may well try to load files
from fredb's home folder.

To be clear, on the same server, if you had three identical files as below

if you had a static HTML file that contained

<script src="/javascript/test.js" />

in the head section and that file had a function called somefunction() in it
then a call to somefunction() would work OK.

If you had and ASPX file that contained

<script src="/javascript/test.js" />

in the head section and that file had a function called somefunction() in it
then a call to somefunction() would work OK.

But...

If you have a masterpage that contained

<script src="/javascript/test.js" />

in the head section and that file had a function called somefunction() in it
then a call to somefunction() within the content section of the child ASPX
page the it would not work.

But in each case the rendered HTML would be the same.

If the above is true then i am at a loss as well, if i have missed something
please point out where i have gone wrong.
 
Hi clickon:

you´re completely right. That´s exactly the problem! I didn´t find a
solution so we report the problem to Microsoft.

We´re hoping their answers! I let you know! And Thanks a lot again!
 
I'd just remove the runat=server from your <head> tag.

Putting the runat=server in your <head> tag will add the ../ to your
<script src> tags.

So when you use /javascript/test.js you should actually get an error
under IIS becuase Asp.Net will make it ..//javascript/test.js or
.../../javascript/test.js or some crazy nonsense.

I'm a bit confused though. Sounds like you are testing on IIS but will
go live under Apache (using modmono?).

Note that if you remove the runat=sever from the <head> you will also
probably have to reference any CSS in your head section as opposed to
using the StyleSheetTheme attribute in your page declaration. (did that
even makes sense?....)
 
Back
Top