简体   繁体   中英

How to run a javascript function when a page dynamically loads content

I am currently writing some Javascript code that adds some tags across text in an HTML file after the page loads. I use the

window.onload

method for achieving this.

However, I am facing an issue in pages like google plus, where you get more content as you scroll down. Is there a way of calling my JS function when such a page adds more content?

Thanks

Akshay

There are several ways how to get this working. You can either use jquery like this:

$('#mydiv').bind("DOMSubtreeModified",function(){
    // your code goes here
    alert('changed');
});

Note that this is not supported in IE8( and below).

Or you can run loop and continuously fire the desired code:

var interval = setInterval(function() {
    // your code goes here
}, 1000);

Fiddle: http://jsfiddle.net/8RF5r/5/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM