tags:

views:

59

answers:

2

I have a response from an AJAX request using jQuery returned as 'response'

I need to compare it with old HTML. If it is different it will just do some css.

The problem is it detects a different change everytime even when there is no difference and there is a continueous loop which makes my animation on css repeat itself.

Anyone help?

var oldHTML = $("#container").html();
$.ajax({  
                url: "ajax/log.php",  
                cache: false,
                dataType: "JSON",
                success: function(response){
if(oldHTML != response){ $("#container").css({"border":"1px solid red"}),$("#container").animate({"border":"none"}); }
                    }

It does not always detect a change or no change. It also loops the animation bit :S

A: 

I would rephrase your question to mention jQuery & AJAX -- took me a second to see the tags.

I would use a file comparison such as WinMerge (free) to see what the difference is.

My guess is it might have to do with spacing, in which case you can use a trim function.

Also, if you post your code it would be helpful.

Kerry
A: 

There might be alterations to the html when its being appended to the DOM. I would store the HTML into the element as a data object as-is to keep a copy of it.

Such as

   // get the saved response, if any, that was saved on the last ajax request
    var oldHTML = $("#container").data('oldHTML');
    $.ajax({  
                    url: "ajax/log.php",  
                    cache: false,
                    dataType: "JSON",
                    success: function(response){
                      if(oldHTML != response){
                         $("#container").css({"border":"1px solid red"});
                         $("#container").animate({"border":"none"}); }
                        // assign the original server response to oldHTML
                         $("#container").data('oldHTML', response);
                        // shove response into the DOM
                         $("#container").html(response);
                       }else {
                          // i guess you're doing nothing here?
                      }
           });

or someting similar, hopefully that conveys the idea

Dan Heberden
How does <pre>var oldHTML = $("#container").data('oldHTML');</pre> get the HTML of #container?
inetech
if it's full ajax, then when it is populated (i took a stab at how you're doing it, see the conditional; when it sets the html, it also assigns 'oldHTML' to the original server-returned data) - if #container is set by the server originally, then after you check for the saved data, you could do if(oldHTML == "") { oldHTML = $('#container').html(); } - but it sounds like whenever the ajax request runs for the first time, you want that html in #container, yes? (made some changes to the above code)
Dan Heberden
Oh thanks, it works on FF, but not on IE :S Help? :)
inetech
Getting any script errors? (check the lower left corner and make sure its enabled in internet options) - is the whole ajax failing or just the conditional? if it's the whole thing, make sure you're including jquery as text/javascript not application/javascript
Dan Heberden
No errors and using text/javascript.I've changed my script to play sound. It loops on IE and i have the problem that my script is messy: if($.data(document.body,'oldHTML') != html){ $('body').append('<div id="soundContainer"><embed src="alert.mp3" autostart="true" loop="false" volume="100" hidden="true" /></div>'), $("#soundContainer").fadeOut(2000, function(){ $("#soundContainer").remove(); }); }
inetech
I would put the whole source into jsbin.com so we can have a look. There must be a different way to go about tracking changes to the body element, though - that seems quite intensive of an 'if' statement, lol..
Dan Heberden
http://jsbin.com/epaho3/2
inetech
http://jsbin.com/epaho3/3/edit - this help?
Dan Heberden
Doesn't seem to append sound
inetech
Ah very good xD , embed appears but no settings now lolAnd weird is that on IE that if statement loops itself???
inetech
Erm, not sure you understand. Looking in the developer thing in chrome, it shows <embed>. No settings?And in IE it repeatedly does that appendTo function and remove it etc.
inetech
what is calling "loadLog" - is IE calling _that_ function over and over?
Dan Heberden
No no, the script inside if(oldHTML != html) {}
inetech
that should only fire on success, which means the ajax call is being fired multiple times. If you do something each time loadLog() is executing (like a $('#consoleDiv').append('<br />Running function'); call) does it only show the single request with a bunch of ajax returns? Something isn't right here...
Dan Heberden
http://jsbin.com/epaho3/6/edit it overwrote for some reason
inetech
i updated the whole thing with .post to make sure your response is html
Dan Heberden
Why .post when we're retrieving info?
inetech
Its not working at all now :/
inetech
Have to go catch ya later
inetech
.post is a wrapper for .ajax, just a bit easier to deal with and formats the return data (in this case, html). Either way, ajax/messages.php is being called and the results handled. There is more to your script/page though, such as why you're checking if html is different instead of just flagging a variable when the data is updated. Does the $('#chatbox') get updated by another function? is this by a user? is it a textarea that could use a 'onchange' event instead?
Dan Heberden
Still here xD$('#chatbox') lists the chat been said
inetech
http://www.cometchatdemo.com/wordpress/ these manage to do it
inetech
I'm going now. Bye
inetech
Any ideas on how to fix this problem?
inetech
We've already tackled how to do a comparison of the html - but if you're using cometchat, you can alter the code yourself (the javascript is 'cometchat/cometchatjs.php' - you can add your own "play sound script" when things happen. In other words, I think you should take a stab at a different way to get the sound to play than comparing the HTML - that there is a simpler, faster, and better way to get your task accomplished.
Dan Heberden