Monday, February 21, 2011

Problem in reference js file for IE7/8

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.

From stackoverflow
  • ~/ 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.

    peanut : but why it works in IE6? I tried Also can't work in IE7/8
    John Boker : 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.
  • 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.

0 comments:

Post a Comment