0

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>
AbsoluteBeginner
  • 2,160
  • 3
  • 11
  • 21
Yoda
  • 319
  • 2
  • 5
  • 20
  • What does the openpopup look like? – mplungjan Jan 04 '21 at 13:47
  • I updated the scrpt.src with a dummy URL. It's actually a call to a third party URL which automatically opens a popup (the vendor handles the popup when a call is made to the URL) – Yoda Jan 04 '21 at 13:52
  • Probably use element.addEventListener('click', functionToCall) rather using .onclick. https://stackoverflow.com/questions/6348494/addeventlistener-vs-onclick – tsfahmad Jan 04 '21 at 14:58

0 Answers0