0

I'm trying to check a page (on the same domain) for a specific string and then execute something accordingly. How can I go about this in JavaScript (with jQuery loaded)?

A (maybe too much) simplified schematic:

url = "pageToLoad.php"

if(StringOnPage(url) == TRUE){
    // Do a bunch of stuff
}else{
    // Do nothing
}

Now how would I construct StringOnPage() ideally? I made several attempts with jQuery's .load and .ajax, I even tried to load it into a hidden container. There must be a way to load the page into a string and check for an expression or something without all the html hacks.

The page is just an HTML populated file. Basically I need to find a text in a DOM element.

Pylsa
  • 298
  • 1
  • 2
  • 15

1 Answers1

2

Load the page via AJAX as a plain string and then simply check if the string you are looking for is somewhere in the string you got from your AJAX call:

$.get(url, function(data) {
    if(data.indexOf('whatever') != -1) {
        // do a bunch of stuff
    }
}, 'text');

Of course you could also use 'html' instead of 'text'; then data is a jQuery object containing the DOM of the page you just loaded.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • Maybe a lot to ask, but I got this far already. How would I integrate this into my structure? – Pylsa May 15 '12 at 21:50
  • You cannot use it synchronously. Everything that relies on the result of the `if` from your question needs to go into that callback. – ThiefMaster May 15 '12 at 21:51