I have an iFrame in a page and below code is located in the iFrame. I'm trying to open a popup based on the button click however there seems to be some issue with the code. The button click only works once after the initial page load and clicking on the button after that doesn't seem to trigger the code/ open the popup. I tried to remove the window.onload to make sure the click event is always triggered, however i see a " Cannot set property 'onclick' of null" error since the click loads before the DOM is ready.
What am i missing here?
window.onload = function() {
document.getElementById('opennewsletter').onclick = function() {
var scrpt = document.createElement('script');
scrpt.type = "text/javascript";
scrpt.src = '//s3.amazonaws.com/js/openpopup';
scrpt.setAttribute("async", "async");
scrpt.setAttribute('crossorigin', "");
document.body.append(scrpt);
}
}
<div>
<a id="opennewsletter">Newsletter</a>
</div>