Monday, November 17, 2008

Microsoft JScript runtime error object expected with jQuery

I was trying to get a simple piece of jQuery to work and was running up against this error in Internet Explorer (IE):

Microsoft JScript runtime error: object expected

The problem turned out to be that I wasn't referencing jQuery to the correct path. The trick I discovered, if you're using Visual Studio, is to just drag the JavaScript file from the Solution Explorer into the <head> element on your page and it will insert the correct reference for you.

UPDATE (9 July 2009):

I am referencing jQuery from the Google CDN now:

<script src="http://www.google.com/jsapi"></script>
    <script>
        google.load("jquery", "1.3.2");
        google.load("jqueryui", "1.7.2");
</script>

Using this technique will also get rid of this error and improve performance.

The first reference to jQuery (1.3.2) is for the core jQuery library. The second reference to (1.7.2) is for the optional jQuery UI library that you will only need if you are using the UI components/elements of the jQuery library. If you are using jQuery to manipulate the DOM only then you won't need the UI reference.

 

20 comments:

  1. make sure the script tag in the head tag that references jquery ends with '</script>' and not '/>'. the same as dragging the script file onto your markup
    THX

    ReplyDelete
  2. Search the problem , your page came up.
    Worked for me ... thank you.

    ReplyDelete
  3. Thanks Amjad and Ellis, found you after breaking my head for nearly 10 hours on this error and your solution worked. You guys rock!!!!!

    ReplyDelete
  4. Amjed, half a day of my precious life already had gone down the drain trying to get Jquery to work in a dummy Asp.net web app. Your information saved further time being lost, much appreciate it.
    Ellis, thanks for sharing info on using declarative syntax to refer to a particular version of jquery.

    ReplyDelete
  5. Good Information, saved my time.
    Thanks Amjed and ellis.

    ReplyDelete
  6. Worked for me! Thanks very much

    ReplyDelete
  7. I spent so much time trying to 'debug' this and with a simple drag and drop, my link now works!

    ReplyDelete
  8. You hit the error eactly. It hlped me a lot. thanks

    ReplyDelete
  9. Just can not THANK you enough.
    This stupid error drove me crazy for days.

    ReplyDelete
  10. Thanks a million .... ! I could help my friend solve this issue which has been consuming him for days :)

    ReplyDelete
  11. Thanks a lot ... I have spent so much time before I found your usefull page!!!!!

    ReplyDelete
  12. I am having the same issues using Internet Explorer 8. FireFox has no issues. Any project i use for Jquery or silver light has issues
    I downloaded open source projects from http://jqmvcgrid.codeplex.com/ . Give the same error "Microsoft JScript runtime error: '$.jgrid' is null or not an object"
    Another application from blog.maartenballiauw.be/.../CarTrackr-Sampl gives the same error.
    Any advice would be helpful

    ReplyDelete
  13. I have this problem in ie 6 but when I copy the .js files to local machine and refer it in the script tag it works fine.
    I am trying to make use of ajax.aspnetcdn.com/.../jquery-1.4.4.js through my visual studio editor and facing this problem.
    As per your guidance if I am referencing "http://www.google.com/jsapi" then from where should I drag this?. if its a local file then I can drag it from the solution explorer but if am referencing google or aspnetcdn then how do I drag that URL?
    Please help.

    ReplyDelete
  14. @Saami - If you're trying to get intellisense in Visual Studio then you need a local copy of the jquery-1.4.1-vsdoc.js file and you would drag that onto your view.
    If you're just trying to use jQuery in a production style environment then you would copy the above code into your .master file or view.

    ReplyDelete
  15. perfect...thank you very much. Drag n drop did not work for me but adding links from google did.

    ReplyDelete
  16. Thanks a lot, after dragging the jquery its works fine

    ReplyDelete
  17. Of course, on my development machine had high security settings so jquery would not work!
    Reset IE security settings a bit lower and it worked!
    A good tip if your developing on server 2008 which has high security within IE by default.

    ReplyDelete
  18. Thank you for this article. I just want to notice that make sure you don't forget to include you jquery.min file, because I forgot and it did not work.
    So final solution:
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
    google.load("jquery", "1.3.2");
    google.load("jqueryui", "1.7.2");
    </script>
    <script type="text/javascript" src="path/to/your/jquery/file/jquery.min.js"></script>

    ReplyDelete
  19. Thanks, it worked. I later found that if you don't want to reference an outside link, you can try adding
    <configuration>
    <system.web>
    <authorization>
    <allow users="?"/>
    </authorization>
    </system.web>
    </configuration>
    to your Web.config. If you're already blocking anonymous users, one trick is to create a second Web.config in your Scripts folder with that authorization.
    Courtesy of www.brianhawkinstech.com/.../144-jquery-erro

    ReplyDelete
  20. hi,
    great post and it's worked fine for me
    bhaskar, csharpektroncmssql.blogspot.com

    ReplyDelete