views:

68

answers:

2

I have a very strange problem in an ASP.NET page, the following reference of JavaScript file works well in IE6

<script src='~/Scripts/xxx.js'  type="text/javascript"></script>

But not working in IE7/8, I got object required error when load the page change to the following works:

<script src='<%# ResolveUrl ("~/Scripts/xxx.js") %>'  type="text/javascript"></script>

Can anyone explain? Thanks.

+1  A: 

~/ is not going to be anything the browser knows about, it needs to be a relative path or an absolute one. The ResolveUrl method take an asp.net path and creates one the browser can understand.

John Boker
but why it works in IE6? I tried <script src='../Scripts/xxx.js' type="text/javascript"></script> Also can't work in IE7/8
peanut
I would say it works in IE6 because IE6 is not standards compliant, IE7/8 are more standards compliant. what is the location of the page this is on and what is the location of the script. i usually use absolute paths to my scripts so URL rewriting and other factors don't affect it.
John Boker
+1  A: 

Use this:

<script src="<%= Response.ApplyAppPathModifier("~/Scripts/xxx.js") %>" type="text/javascript"></script>

Basically, the "~" is a way to refer to the application's home directory, but it is only understood by ASP.NET. You can call the function I listed above to translate that into a real HTTP path.

John Gietzen