Consider there is some hyperlink tag that makes the API call to backend.
Example:
After the backend API call completed it will trigger/open a popup in that page.
Popup data: (Sample one div data)
Contact Name
My objective is to extract data from this popup (above tag). Lets say Contact Name from h1 tag.
what I tried so far:
let atag = document.getElementById("ember107");
atag.addEventListener('click', () => {
document.getElementById("pv-contact-info").innerText; // getting from popup h1 tag
});
atag.click(); // explicit click
The Problem I faced is Uncaught TypeError: Cannot read property 'click' of null when this statement is executed document.getElementById("pv-contact-info").innerText;
I know the problem is popup content was not loaded completely that's why this code document.getElementById("pv-contact-info") returning null.
My Question is whether there is any listener function to check Popup content is loaded
completely or we can do this in another approach. Most preferable using browser support /vanilla javascript rather than library.
解决方案
You can use a MutationObserver to watch for changes to the DOM on the page. MDN.
If you need to stay compatible with older browsers use your click event to trigger your own manual watcher. Something like:
var interval_id = false;
function lookForPopup(){
if(interval_id){
clearInterval(interval_id);
}else{
interval_id = setInterval(function(){
var popup = document.getElementById('some-id');
if(popup){
// do something
clearInterval(interval_id);
}
},1000);
}
}