I am writing some jquery script that will run across a large amount of completely different web pages (different domains too) and it will surround certain words on the page with html tags to style them up - make them more noticeable etc. Here's some sample code I've written...

$('body').ready(function() {
var body = $('body').html();
var matches = body.html().match(/a complex regular expression here which matches a variety of things/g);
var match;

for (var i=0; i<matches.length; i++)
  {
    match = matches.pop(); // go through each matched instance
    body = body.replace(match,"<h1>"+match+"</h1>"); // style each matched instance in the document body
  }

$('body').html(body); // set it back onto document body
});

The problem is, certain domains on which this script is to be run on - on 'body'.ready they only load very basic stuff and load nearly all their content afterwards using ajax/javascript.

So at line 2 of my code where I retrieve the body's html it only contains the bare bones for some of these webpages at this time therefore later when the matched instances are replaced and put onto the DOM the changes are made but on a page which was only half-loaded.

I hope this makes sense. If you know a workaround for this then please share.

pixelsoul, besides live() being deprecated, there is no event that you can use with it to know when an content has been changed.

The change() event is only for inputs, textareas and selects.
The load() event is only for window, img, scripts and frames.

So I don't see any use of live() is this operation.

But maybe I'm forgetting something, who knows =P

Ah... you're right. I didn't even see that it had been deprecated. It looks like they moved to .on()

I only mentioned it because I remember a time I was watching someone build a UI that was mostly ajax calls, and I thought I remembered them saying something about a way to affect the content that gets rendered after the page has loaded. I thought it may have been .live() but it was quite a while ago.

There's .on(), and then there's also .one() when you only want it to execute once and then unbind itself after.

I'm going to have to play around with .on() later and tests the behavior on asynch stuff.

Replace your $('body').ready( with $(document).ajaxStop(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.