views:

39

answers:

3

I use the following jQuery (1.4) code to test whether cookies are accepted:

$.get("http://localhost:8080/cookietester/cookietester", function(data) {
    if (data == "false")
        document.write("Activate cookies!");
    else if(data == "true")
        document.write("ok");
});

But the browser signals that it doesn't stop page loading. Google Chrome 5 doesn't execute the script properly and doesn't display anything.

Is something wrong with this code?

A: 

There might be another javascript code still running ?

Kaaviar
No, there is only this script on the page.
deamon
+2  A: 

Don't call document.write there.

Calling document.write after the page is finished loading will reset the page, which is not what you want.

Instead, use jQuery to set the text() of an element.

SLaks
+3  A: 

You can't use document.write in that scenario (actually I avoid it whenever possible), instead use .append() to add a message to the <body>, like this:

$.get("http://localhost:8080/cookietester/cookietester", function(data) {
  if (data == "false")
    $(document.body).append("Activate cookies!");
  else if(data == "true")
    $(document.body).append("ok");
});

Or, you could just alert it:

$.get("http://localhost:8080/cookietester/cookietester", function(data) {
  if (data == "false")
    alert("Activate cookies!");
  else if(data == "true")
    alert("ok");
});
Nick Craver
That's it! This little changed solved all the problems and even Chrome just works fine.
deamon