Thursday, February 17, 2011

jQuery "Object Expected" error in IE

I encountered this problem when prototyping some SharePoint webparts. It actually works well in Firefox and Chrome. Here's the snippet for illustration:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script type="text/javascript" src="../js/jquery-1.5.min.js"></script>

<script type="text/javascript">

    $(function() { // <=> $(document).ready(function() {
...
By adding one line of code in line 7 before $(function(),
alert(typeof $);
it prompts "undefined", meaning jQuery is not loaded by IE. The solution is quite simple, IE->Tools->Internet Options->Security, by default it is High, either change it to Medium, or use custom level then enable Scripting->Active scripting (IE8). Now it alerts "function" and jQuery is working nifty then.

Ref: jQuery $(document).ready() failing in IE6

4 comments:

Anonymous said...

I'm getting that error on our production site, but it works fine on the dev site. What's throwing the error is

$(document).ready(function () {
});

Do you know if there is something in SharePoint settings or permissions that needs to be changed on the production site?

Thanks.

Anonymous said...

And the code runs fine on the production site in Chrome.

marshal said...

Assuming you were using the same browser to access your two sites and one has problem and one doesn't, then probably you should check the following:

1. Client. Most likely in your IE security, dev site is trusted (maybe in the same domain) while production site is not.

2. Server. Check for that site if Javascript is allowed to run in IIS.

Anonymous said...

Thanks.

Turns out the problem was accessing the jquery js file from the production site. So we downloaded the mini file and added it as a resource to the SharePoint site and the problem was solved.