codepuppy
codepuppy

Reputation: 1140

? Is there a known issue with Internet Explorer and Jquery's $("document").ready(function()?

This is a generic question so not really any code to show in this question.

I am having a problem getting a particular jquery script to run in IE9 (indeed any version of IE)

Most annoyingly it appears to be slightly hit or miss which is making debugging this very difficult.

In FF and Chrome it runs completely without issue.

But in IE the script will only run if you press F5 to refresh.

Obviously I am using

$("document").ready(function() {

    alert("start control script");
    // lots more code here
};

By observation it appears that once I have run the script once if I subsequently logout and log back in all is well. But If I close the browser completely I am back to needing to press F5 to get the script started. This is only happening in IE.

I imagine that it works once I have refreshed and logged out due to some kind of caching which is lost when I close the browser completely.

Any thoughts welcomed.

Upvotes: 1

Views: 800

Answers (3)

Zander
Zander

Reputation: 2684

You have written it incorrectly, document is a global object & does not need quotes around it.

$(document).ready(function() {

    alert("start control script");
    // lots more code here
});

Notice the lack of quotes around the word document & the missing parenthesis at the end.

Upvotes: 2

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35963

From jQuery documentation:

All three of the following syntaxes are equivalent:

$(document).ready(handler)
$().ready(handler) (this is not recommended)
$(handler)

There is also $(document).bind("ready", handler), deprecated as of jQuery 1.8. This behaves similarly to the ready method but if the ready event has already fired and you try to .bind("ready") the bound handler will not be executed. Ready handlers bound this way are executed after any bound by the other three methods above.

The .ready() method can only be called on a jQuery object matching the current document, so the selector can be omitted.

The .ready() method is typically used with an anonymous function:

$(document).ready(function() {
  // Handler for .ready() called.
});

Which is equivalent to calling:

$(function() {
 // Handler for .ready() called.
});

If .ready() is called after the DOM has been initialized, the new handler passed in will be executed immediately.

Upvotes: 1

Gurpreet Singh
Gurpreet Singh

Reputation: 21233

You don't need document in quotes also you are missing parenthesis ) at the end.

$(document).ready(function() {

    alert("start control script");
    // lots more code here
});

Upvotes: 1

Related Questions