1

I have tried running a simple html file which includes a jquery.js file with a jquery click() alert, when I manually open the html file in a browser the jquery/javascript works fine.

However if I host this html (or any aspx) file in IIS 7.5 everything else works including CSS and html however the jquery javascript functions from the .js do not work. I have a clean install of ASP.NET, IIS 7.5 and Visual Studio 2010 but no luck.

I looked in the applicationHost.config file and .js files seem to be there and enabled. I am running the application pool for the IIS website in Integrated mode .net 4.0. The weird thing is that Visual Studios built in Cassini web dev server also has the same problem and won't server .js files. This is what is in my web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
   <system.webServer>
       <urlCompression doStaticCompression="true" />
    </system.webServer>
</configuration>

Any ideas? Here is the html, I've tried it with the google cdn jquery js too:

<html>

<head>

<script src="Scripts/jquery-1.7.1.min.js"     type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function(){
  $("p").click(function(){
    $(this).hide();
   });
});

</script>

</head>

<body>

<p>If you click on me, I will disappear.</p>
<p>Click me away!</p>
<p>Click me too!</p>

</body>

</html>
User101
  • 748
  • 2
  • 10
  • 29
  • Did you checke that the javascript is not disabled in the browser?! – gdoron Feb 16 '12 at 22:06
  • Could you please provide html of your page? – Evgeny Lukashevich Feb 16 '12 at 22:12
  • Hi @Eugene and gdoron the html and javascript reference to jquery works fine in all browsers when I drag it manually into the browsers. However its only when its hosted by IIS/VS web server that it won't serve it. I even tried using the google CDN jquery but to no luck. So I don't think there is a problem with the code, its certainly setup either of asp.net or my web.config. – User101 Feb 17 '12 at 09:17
  • @Eugene added html to original post – User101 Feb 17 '12 at 18:58
  • Any error messages in the console? There must be some if jQuery is not being included – Pekka Feb 17 '12 at 19:03
  • Your code works. Look here (http://jsfiddle.net/wfbY8/). Make sure to look at your console errors or fiddler. Your reference to they jquery file is probably returning some sort of error(probably 404) where it can't find it. – jnoreiga Feb 17 '12 at 19:28

2 Answers2

1

I reinstalled IIS and all was well, though that required a restart which maybe I hadnt done the first time around!

User101
  • 748
  • 2
  • 10
  • 29
0

I'll guess that your path is resolving to different location, if you are using WebForms it's better to specify path like this:

<script type="text/javascript" src="<%=Page.ResolveUrl("~/Scripts/jquery-1.7.1.min.js")%>">     

For ASP.NET MVC you can use:

<script type="text/javascript" src="<%=Url.Content("~/Scripts/jquery-1.7.1.min.js")%>">
Evgeny Lukashevich
  • 1,500
  • 14
  • 26