views:

708

answers:

5

This relates to my previous post:

http://stackoverflow.com/questions/2318696/jquery-load-method-causing-page-refresh-ajax

I changed my implmentation to use the .ajax method instead of .load and it works fine in Firefox but not in IE7 or IE6:

    $('ul#coverTabs > li > a').live('click', function(event) {

    // Find href of current tab
    var $tabValue = $(this).attr('href');

    $.ajax({
        type: "GET",
        cache: false,
        dataType: "html",
        url: $(this).attr('href'),
        success: function(data){

        $(data).find('.benefitWrap').each(function(){

            var $benefitWrap = $(this).html();

            $('.benefitWrap').replaceWith($('<div class="benefitWrap">' + $benefitWrap + '</div>'));

        });

       }

    });

    event.preventDefault(); 

});

This is killing me as it has taken ages to get this far.

Any ideas where i am going wrong?

+4  A: 

Just an quick though. I have had some frustrating issues with jQuery in the past that silentely failed with IE6/7. In almost all cases, somewhere in my code (not necessarily in the ajax call in question) I had a extra comma after an argument like this:

$.ajax({
        type: "GET",
        cache: false,
        dataType: "html",
        url: $(this).attr('href'),
        success: function(){},
)}

I would run the script through jslint to see if there is anything funny in the syntax causing this issue before doing proper debugging.

Corey Downie
yes, i know that IE6 does throw an Javascript Error, when there is a comma after the last parameter
choise
+1  A: 

event is a reserved word in some versions of IE. Try changing the parameter you're capturing from event to something sure to avoid collision, like evt, e.g.:

$('ul#coverTabs > li > a').live('click', function(evt) {

  evt.preventDefault();

  // Find href of current tab
  var $tabValue = $(this).attr('href');

  $.ajax({
    type: "GET",
    cache: false,
    dataType: "html",
    url: $(this).attr('href'),
    success: function(data){

    $(data).find('.benefitWrap').each(function(){

        var $benefitWrap = $(this).html();

        $('.benefitWrap').replaceWith($('<div class="benefitWrap">' + $benefitWrap + '</div>'));

    });

   }

});

Update

Upon further review, I believe your problem is the find(). In this case, you should use filter().

success: function(data) {
  $(data).filter('.benefitWrap').each(function() {
    // This should accomplish the same thing a bit more cleanly.
    $('.benefitWrap').html(this.innerHTML);
  });
}

That can be further refactored down to just:

success: function(data) {
  $('.benefitWrap').html($(data).filter('.benefitWrap').html());
}
Dave Ward
@ Dave.Thanks for this but neither seems to work. The first does not return anything from the server in either FFOX, IR7 or IE6 and the second just clears all data from the 'benefitWrap' div.Cheers anyway.
RyanP13
Sorry that sounded a little rude. Really appreciate the help. I'm sure it is just an IE quirk/crpaness. Not gonna wave the white flag just yet.
RyanP13
I believe you're seeing the second clear the div out only because it's the first time the loop is finally running. I tried with static data mimicing what you've described as returning from the server, and `filter` worked where `find` didn't (`find` found 0 items and didn't enter the loop, like you described in your followup answer). Are you absolutely sure that the HTML you think is coming back from the server is actually coming back? It sounds like there's either no .benefitWrap div in the returning markup or it's empty.
Dave Ward
Yeah i don't think it is coming back in IE. Appears as though some others have difficulty in IE with this sort of thing.
RyanP13
A: 

Well the problem seems to be here:

success: function(data){    
            alert(data);
            $(data).find('.benefitWrap').each(function(){
                alert(data);
                var $benefitWrap = $(this).html();
                $('.benefitWrap').replaceWith($('<div class="benefitWrap">' + $benefitWrap + '</div>'));                
        });

The second alert never appears but the first does. I hate IE!

RyanP13
So what is in |data|? And what is the length of |$(data).find('.benefitWrap')|?
janmoesen
|data| should contain HTML from the index page on the server. This is a JSP page that accepts URL variables, queries the database and updates a HTML table which is then called by the AJAX and returns the results to the smae page that called it.
RyanP13
See the update to my answer, based on this.
Dave Ward
+1  A: 

I accidentally worked out what the issue was.

The link referenced in this variable:

var $tabValue = $(this).attr('href');

Had a hash value on the end like so:

https://bupacouk.bwa.local.internal.bupa.co.uk/cash-plan-quote/quoteAction.do?getBenefitLevelDetails=getBenefitLevelDetails&amp;productPolicyId=7850#a1

This cause the AJAX to fall over in bothe versions of IE.

Using the following code:

var $withoutHash = $tabValue.substr(0,$tabValue.indexOf('#'));

Getrs rid of it and it now works :)

RyanP13
+1  A: 

Hash in the url is an issue with IE 6 and 7, xhr.status eror 500. Resolved well with:

function removeHash(url) { return url.replace(/#(.*)$/, ""); }
$.get(removeHash(this.href),...)

see: http://forum.jquery.com/topic/ticket-3808-ajax-load-fails-404-if-there-is-a-hash-in-the-url

oori