开发者

Browser doesn't stop loading with jQuery $.get

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

$.get("http://localhost:8080/coo开发者_如何学运维kietester/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?


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");
});


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.


There might be another javascript code still running ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜