2

I'm looking to use jQuery to determine if the current page has changed upstream, and if so, perform some action.

I want to do this by using jQuery selectors over the data returned by an AJAX call (for the purposes of this question, my "page has changed" metric will be "has the content of first <h1> changed").

Thus I find myself wanting to use jQuery selectors over the HTML returned by an AJAX get(). The "best" "solution" I've found thus far is appending the data to some hidden div, and using a selector over that, as below:

var old_title = $('h1').html();

$.get(url, function(data) {
    $('#hidden_temporary_storage').append(data);
    var new_title = $('#hidden_temporary_storage h1').html();
    if (new_title !== old_title) {
        do_something();
    }
});

This feels so very wrong - I'd be nesting html / head / body tags, and there would be id collisions et cetera.

I have no control over the upstream page, so I can't just "get the data in a more convenient format" alas.

3
  • Ideally, you would avoid querying for data you don't need. If possible, try tailor the answer to the request to meet your needs. Commented Jan 9, 2012 at 13:23
  • As per the last line "I have no control over the upstream page" Commented Jan 9, 2012 at 13:51
  • arghh... when will I learn to read! My bad :) Commented Jan 9, 2012 at 14:01

2 Answers 2

3

You can do:

var x = $('<div>'+data+'</div>').find('h1').html();
// use x as you like

i.e. you don't need to append the returned data to your page to be able to get properties and values from HTML elements defined within it. This way there would be no id collisions, multiple head/body tags etc.

Sign up to request clarification or add additional context in comments.

6 Comments

I just tried that and it doesn't work. $('<html><body><div class="toto">bob</div></body></html>').find('.toto').html() (returns null)
$() strips off the head, body etc.. and returns only the list of elements inside the body of the HTML string. For example: $('<html><body><h1>Hello</h1><div class="toto">bob</div></body></html>') returns a list of 2 objects namely a h1 and div with class toto.
Yeah and if you try to find something in it $(htmlstring).find('.myclass') it will return null ;)
Updated the answer to correct the issue pointed out by Jean-Philippe Gire
I'm still experiencing the same issue as Jean-Philippe - every time I find() I get null...
|
1

I think your best bet here is to use an iFrame. And then use jQuery on the content of that iFrame.

var old_title = $('h1').html();

$.get(url, function(data) {
    $('<iframe id="tmpContent"></iframe>').appendTo("html");
    $('#tmpContent').contents().find('html').html(data);
    var new_title = $('#tmpContent').contents().find('h1').html();
    if (new_title !== old_title) {
        do_something();
    }
    $('#tmpContent').remove();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.