开发者

In javascript, how can I call a function anytime someone updates the page?

开发者 https://www.devze.com 2023-01-22 22:54 出处:网络
I want to run a javascript function anytime other javascript on the page is run.This is to be able to post-process the dynamically generated 开发者_如何学Ccontent.I can\'t simply do the post-processin

I want to run a javascript function anytime other javascript on the page is run. This is to be able to post-process the dynamically generated 开发者_如何学Ccontent. I can't simply do the post-processing after the page loads, because I need to apply my post-processing to the content that is generated by javascript that does load another page.

Alternatively, can I register a callback to be called when a click is done on any part of the page and after all the other onclick events have been processed?

I'm doing this for a greasemonkey script to make an internal web page more usable.


You may use one of the events DOMNodeInserted or DOMSubtreeModified to identify something changing on the page.

And you may attach an onclick event to the window.
Alternatively, you could only attach it on elements with the inline attribute onclick in it.


One of the (few) options to solve this is to use a setInterval() that polls for changes to the document... There are not solid cross-browser events that will inform you of document changes.

If you are looking for changes / elements added to a <div> for instance you could use the .innerHTML of that div and compare length, etc to search for new elements.


If you use jQuery, you can run your function once when the DOM is ready and then bind it to the ajaxComplete event so that it runs anytime a script loads data via AJAX from the server.

0

精彩评论

暂无评论...
验证码 换一张
取 消