44

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

  • 1
    I have the same issue. And NOTHING works, I have tried ALL of the suggested solutions below. :( I am stuck and give up. – Roemer May 27 '15 at 23:56
  • Another try would be setting DOM's `innerHTML` after loading the content with `$.ajax()` as an alternative. – Martin Staufcik Jan 14 '16 at 22:28

15 Answers15

48

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.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
S.Kiers
  • 4,086
  • 3
  • 32
  • 38
  • 13
    It'd be better to append a timestap, so `$('.islice').load('home.html' + new Date().getTime());` – EMMERICH Mar 31 '11 at 12:50
  • S.Kiers and EMMERICH: You saved my life. I was trying to figure out why the **** IE was not loading my initial start-page with the back-button of the browser (I do some AJAX-Loading with Hash-tags) and every other site would work as excepted. Adding the Random (or better the Timestamp) worked out :DDD – Shion Mar 01 '12 at 08:31
  • 2
    any chance, if this is the answer, you could mark it as such? I'd appreciate it... – S.Kiers Mar 02 '12 at 17:08
  • 3
    should it be $(".islice").load("home.html?" + new Date().getTime() ); instead? I mean, missing the '?' following home.html – bcbishop Mar 03 '14 at 02:53
16

$.ajaxSetup({ cache: false });

This will clear cache in IE and .load() will work. I've tried it.

13

If the HTML that you are loading is broken, jQuery's .load() will not work in IE.. It was the problem for me. After I fixed the HTML , everything worked great in IE also !

Neno
  • 727
  • 3
  • 17
  • 34
  • My URL loads fine in IE directly, but the $('#id').html(data) wont display anything. Alas no errors either. – ericslaw Jun 07 '13 at 21:00
6

I encountered this problem and was scratching my head all day long. However finally found a work around and realized what a weirdo IE is.

First of all,

$(".islice").load("home.html"); 

won't work no matter how hard we try. We will instead have to use

$.get("home.html", function (data) ....... ); 

I will explain the "....." because a usual

$.get("home.html", function (data) { $(".islice").html(data); }); // doesn't work

won't work.

Instead

$.get("home.html", function (data) { 
    data = '"' + data + '"';    
    $(".islice").html(data);
    var newHTML = $('.islice').html();
    $('.islice').html(newHTML.substr(1,newHTML.length-2));
}); // works

will work.

Explanation: => data may have new line chars. so setting innerHTML = data; breaks because of them. By adding quotes we convert it into a string but making that html adds extra quotes so I get rid of the quotes again.

Moral: => IE sucks.. Nothing else..

Sharad
  • 963
  • 11
  • 24
4

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).
David Riccitelli
  • 7,491
  • 5
  • 42
  • 56
4

I found the .load() function did not work very well with IE, but using $.get() instead worked perfectly, e.g.

var dummy = new Date().getTime();
$.get("home.html" + dummy, function(data) {
   $(".islice").html(data);
});
Tim Lloyd
  • 41
  • 1
  • Tried this and it is useful to confirm that the fetch is working. Alas the .html(data) line silently does nothing. – ericslaw Jun 07 '13 at 21:00
  • I think you forgot the question mark here. `$.get("home.html?" + dummy, function(data) {`... – Martin Sep 13 '18 at 08:18
3

I think the problem occurred because the ambiguous encoding. Try to specify the response encoding explicitly (that is, the charset in the HTTP Header), something like:

<meta charset="utf-8">
BrenBarn
  • 242,874
  • 37
  • 412
  • 384
Kevingo Tsai
  • 629
  • 7
  • 21
3

Ok guys... I had same problem with ie 8 and older. This is my solution, hope it helps somebody:

1) At first it's pretty hard to debugging ajax in IE. Why? Console is not upto the mark, but there is another bigger problem - caching. First time you load something wrong it stays in cache. Than you spend 2 hours fixing problem seeing same result (when you do this 1st time). Thanks to this article (and discusion): http://zacster.blogspot.cz/2008/10/jquery-ie7-load-url-problem.html I customized my ajax calls like this:

$(container).load(link +'?random=' + Math.random()*99999 + ' .post-list li', function() { // do some stuff }

Random URL works great

2) @Neno is right! IE have problems with mistakes in HTML. Validate your loading HTML http://validator.w3.org/

arne
  • 4,514
  • 1
  • 28
  • 47
Marek Fajkus
  • 589
  • 4
  • 14
  • Thanks for the tips. I was stuck with IE loading an ajax call. It turned out I had a left over tag which caused the html to not validate. Adding the math.random to the url took care of the IE caching. Thanks again. – garyv Mar 19 '13 at 16:30
3

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');

Tim
  • 6,281
  • 3
  • 39
  • 49
2

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.

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
  • 3
    -1 jQuery extends the event object to make e.preventDefault(); be cross browser. – Paolo Bergantino Jun 30 '09 at 03:23
  • 2
    http://docs.jquery.com/Events/jQuery.Event#event.preventDefault.28.29: Prevents the browser from executing the default action. Use the method isDefaultPrevented to know whether this method was ever called (on that event object). Fixed where necessary (IE). – Paolo Bergantino Jun 30 '09 at 03:25
  • I did not know that they had updated it to work with IE as well - it didn't last time I checked. However, adding return false; should still solve the problem. – Tomas Aschan Jun 30 '09 at 10:26
1

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();
});
Andy Fleming
  • 7,655
  • 6
  • 33
  • 53
1

To prevent having IE bugging on that, add a math.random() parameter to it so it doesn't use this unrelevant cache...

0

You're .load()ing into a <table>?

Hmm... Maybe push the .islice class up a level, into the <td>, or maybe a <div> in between...

(Not that that's necessarily the issue, but it's a possiblity...)

Stobor
  • 44,246
  • 6
  • 66
  • 69
  • That depends entirely on what he's returning from serverside. If the result is just a collection of table rows, without the wrapping tag, it will work just fine the way it is.
    – Tomas Aschan Jun 30 '09 at 10:27
  • @Tomas Lycken: I had checked before I responded; he's sending back divs, or full tables. – Stobor Jun 30 '09 at 11:35
  • @Tomas Lycken: infact, I had even checked with the script debugger: the content was getting added inside the table tag, but it wasn't getting rendered to screen. – Stobor Jun 30 '09 at 11:36
0

I have same problem, for me work add in head

<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
-4

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...
sth
  • 222,467
  • 53
  • 283
  • 367
Stuart
  • 11
  • 1
    PHP doesn't maintain state (unless you directly access $_SESSION or some other session data store), and even if it did it would have no effect on IE, as IE isn't privy to the PHP code on the server. – eyelidlessness Feb 10 '11 at 03:08