views:

19754

answers:

17

I am working on a ASP.net project created with local file system settings. I am using MVC and Jquery. Jquery is working fine when I run the application in debug mode i.e. in ASP.net Development server. I am trying to host the application in IIS 7. In hosted mode, it does not recognize Jquery and gives scripting error 'Jquery is undefined'. The locations of the script files is unchanged in both modes. Can anybody have any clue what can be the reason and how to solve this.

My code look like this;

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script src="../../Scripts/MicrosoftAjax.debug.js" type="text/javascript"></script>

<script src="../../Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>

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

<!-- YUI Styles -->
<link href="../../Content/reset.css" rel="stylesheet" type="text/css" />
<link href="../../Content/fonts.css" rel="stylesheet" type="text/css" />
<link href="../../Content/grids.css" rel="stylesheet" type="text/css" />
<!-- /YUI Styles -->
<link href="../../Content/knowledgebase.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">
    //this hides the javascript warning if javascript is enabled
    (function($) {
        $(document).ready(function() {
            $('#jswarning').hide();
        });
    })(jQuery);
</script>

<asp:ContentPlaceHolder ID="ScriptContent" runat="server" />

....

A: 

This usually occurs when you have malformed HTML or JavaScript.

EndangeredMassa
A: 

If there's no errors in your javascript (eg: missing brackets), then it's either not including the jQuery file, or you're trying to run some code which relies on jQuery before that file has loaded.

nickf
How came its working fine on Asp.net application server
Raja
+12  A: 

Try using Firebug 'Net' tab to see if the file gets loaded.

You can also configure Firebug to fail on all errors and see when the error happens as compared to when jQuery gets loaded.

Andrey Shchekin
I did what you said. I neglected to add the latest jQuery to the project. Thanks!
MrBoJangles
+1  A: 

Jquery?!
You mean jQuery?

Javascript is case-sensitive. Jquery is not the same as jQuery.

Jonathan Lonowski
its JQuery is undefined. The case is fine and its working on ASP.net application server.
Raja
The case should be "jQuery" not "JQuery". But if it works on test, this isn't the issue.
EndangeredMassa
A: 

For me, this error typically occurs when there is a true Javascript error in some of my code. This prevents the jQuery.js file from being fully parsed. Look in the Error Console in Firebug, Safari or another debugging-friendly browser.

Aaron Longwell
+3  A: 

After Using the FIrebug, I found out the files were not found as the relative path for the files doesn;t work. TO resolve the problem, we should use the Url.Content Method to reslove the path. For example:

<script src="<%= Url.Content("~/Scripts/jquery-1.2.6.js")%>" type="text/javascript"></script>
Raja
You should encode the opening character for the script tag in this character from < to <
Pim Jager
It would be good to mark Andrey's answer as accepted...
Will Dean
Why didn't the relative path work though? Care to explain what was different?
Kip
+3  A: 

You could also try letting Google host jQuery for you:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"&gt;&lt;/script&gt;

This will avoid having to worry about the relative path to the script, avoid load on your server, and will be faster for users (who probably already have it in their browser's cache somewhere).

Kip
+1  A: 

Common mistakes is that other Jquery Scripts are included first(Jquery.validation.js) before the Jquery.js

Roy Astro
A: 

Raja is right, URL.Content works.

<script src="<%= Url.Content("~/Scripts/jquery-1.2.6.js")%>" type="text/javascript"></script>

Further, to prevent yourself from getting surprised at the moment you are migrating your application to an IIS server, don't use web development server but use IIS when developing.

You can use IIS directly from visual studio by editing the the web properties:

  • Go to the tab 'Web'
  • Check 'Use Local IIS Web server'
  • Click 'Create Virtual Directory'
  • Go to the IIS Manager to disable anonymous access in the created virtual directory if applicable.
Michel van Engelen
A: 

If all else fails, make sure the file has the right permissions set. This just happened to me!

You can either right click the file in your directory and set the permission there, or just create another .js file in Visual Studio and copy and paste the contents over. Worked like a charm. :)

Ozzie Perez
A: 

try

<script src="<%= ResolveUrl("~/Scripts/jquery-1.2.6.js")%>" type="text/javascript"></script>
A: 

The path is wrong. Double check it.

Elzo Valugi
+8  A: 

0 I do not know whether you guys found the solution to your problem or not. I was facing the same problem and going nuts to figure out why do I get "jQuery is undefined" error on the plugins i use. I tried all the solutions i get from the internet but no luck at all.

But, suddenly something splash on my mind that may be the script files should be in order. So, I moved the jquery referece to first position and everything start working like charm.

Remember guys, if you're using any plugins with jquery, make sure you use the folloing order of setting reference to those fiels.

1.reference to the jquery library 2.reference to the other subsequent plug-in (dependant) libraries and so on...

e.g.: 1. "script src="js/jquery-1.3.2.min.js" type="text/javascript"... 2. "script src="js/jqDnR.min.js" type="text/javascript"... 3. "script src="js/jquery.jqpopup.min.js" type="text/javascript"... 4. "script src="js/jquery.bgiframe.min.js" type="text/javascript"...

Always make sure you must put the jquery reference to first and then the subsequent libraries.

Hope, this solves your problem especially when you use with MasterPages. Its very strange that it works no matter what order you use when you don't use MasterPages but when you do, then it somehow requres the proper order.

Good luck and happy coding,

Vincent D'Souza

Solved my problem. Thanks!
John Fischer
+1  A: 

To elaborate on the "text/javascript" answer, "application/javascript" will work in firefox and load jQuery just fine. It won't, however, load the file in IE. Use "text/javascript":

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" ></script>
Dan Heberden
A: 

To add my problem/solution to this long list...

I was using Windows 2003 to test the site in IE7. The site came up fine but jQuery didn't run at all. So I went straight to the http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js and found that ajax.googleapis.com was not on the list of safe sites. After adding *.googleapis.com everything worked fine. Silly oversight, but maybe this will help someone else in a similar situation.

DavGarcia
A: 

Vincent D'Souza's solution worked out. Thanks.

sds
A: 

Vincent D'Souza,

You are absolutely right ! The order matters ! It solved my error 'jQuery' is undefined.

Thanks!

jaz