簡體   English   中英

對於使用XMLHttpRequest更改其內容的頁面,合適的事件偵聽器是什么?

[英]What is the appropriate event listener for a page that change its content using XMLHttpRequest?

通過在C#中使用GeckoWebBrowser,是否仍然可以知道頁面何時使用XMLHttpRequest完成更新其內容? 我以為DocumentCompleted事件會做到這一點,但是由於該頁面沒有重新加載,因此不會啟動...

您可以使用一個變異觀察者來監視document.body的子樹,並假設在收到最后一個通知后20毫秒,頁面已完成修改(例如)。

在JavaScript中,如下所示:

 (function() { var observer; // The click handler that appends content after a random delay document.querySelector('input').addEventListener("click", function() { if (!observer) { hookupObserver(); } setTimeout(function() { var p = document.createElement('p'); p.innerHTML = "New content added at " + Date.now(); document.querySelector('div').appendChild(p); }, 500 + Math.round(Math.random() * 500)); }, false); // Watching for subtree mods to `document.body`: function hookupObserver() { var timer = 0; observer = new MutationObserver(function() { clearTimeout(timer); timer = setTimeout(done, 40); }); observer.observe(document.body, {childList: true, subtree: true}); } function done() { timer = 0; alert("Modification complete"); } })(); 
 <input type="button" value="Click to simulate async modification"> <div>This is the page</div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM