jQuery's .load() not working in IE - but fine

2019-01-01 15:53发布

I'm hitting my head on the wall against this one ...

I have the following code:

$("#home").click(function(e) {
    $(".tabs").attr("src","tabs-home.gif");
    $(".islice").hide('fast');
    $(".islice").load("home.html");
    $(".islice").show('fast');  
    e.preventDefault();
});

It works perfectly fine in Firefox, Safari and Chrome, but IE only runs attr() and does not do either the hide/show or the load. I tried removing the hide and show and it still does not work.

IE reports no syntax errors, even with DebugBar. What could I be doing wrong?

You can see the live site at http://www.brick-n-mortar.com

15条回答
深知你不懂我心
2楼-- · 2019-01-01 16:16

I had the same problem with IE9.

All ajax requests die silently by default. By using http://api.jquery.com/ajaxError/ I was able to determine the type of error by logging the exception message: An error with the code c00ce56e.

It turns out that this means the response is not being passed utf-8 encoded, as it should be in response to an ajax request.

Turns out I had an typing error in header('Content-type: text/html; charset=utf-8');

查看更多
爱死公子算了
3楼-- · 2019-01-01 16:17

I am having the same problem. Many sites I have found have suggested that IE may be caching your code and suggest to append the code to

$("#home").click(function(e) {
    $(".tabs").attr("src","tabs-home.gif");
    $(".islice").hide('fast');
    $(".islice").load("home.html?" + new Date().getTime() );
    $(".islice").show('fast');
    e.preventDefault();
});

This should ensure that a IE isn't caching.

See http://zacster.blogspot.com/2008/10/jquery-ie7-load-url-problem.html for more info.

查看更多
闭嘴吧你
4楼-- · 2019-01-01 16:19

I was having a similar issue and was able to make it work this way:

.load() and .html() don't work very well in IE; especially if you don't have valid HTML.

$("#home").click(function(e) {
    $(".tabs").attr("src","tabs-home.gif");
    $(".islice").hide('fast');
    $.ajax({
        url: "home.html",
        success: function(data, textStatus, xhr) {
            $(".islice")[0].innerHTML = data;
        }
    });
    $(".islice").show('fast');  
    e.preventDefault();
});
查看更多
低头抚发
5楼-- · 2019-01-01 16:22

I found this workaround to be working:

$.ajax("loaded.html", {
    cache: false,
    success: function(data, textStatus, jqXHR) {
        $("#content-1").html(data);
    },
    dataType:"html"
});

where:

  • "loaded.html" is the URL to the file to load.
  • $("#content-1") is the element that will contain the loaded data (and scripts).
查看更多
素衣白纱
6楼-- · 2019-01-01 16:23

If load is with PHP, reset your array values. For example:

$result = ''; // do this
$row = ''; // do this
$data = ''; // IMPORTANT Kills odd behavior CACHE FOR IE

$result = mysql_query("your sql here");
while ($row = mysql_fetch_array($result)){          
$data[] = $row ..... blah blah blah...
查看更多
姐姐魅力值爆表
7楼-- · 2019-01-01 16:24

The e.preventDefault() won't make any difference in IE - you'll have to use return false; to stop things from happening:

$("#home").click(function(e) {
    $(".tabs").attr("src","tabs-home.gif");
    $(".islice").hide('fast');
    $(".islice").load("home.html");
    $(".islice").show('fast');  
    e.preventDefault();
    return false;
});

To debug this in detail, take a look at Firebug.

查看更多
登录 后发表回答