timing issue when injecting javascript to iframe

29 Views Asked by At

I'm using the below code inject some javascript into an iframe. It works fine however there's a (possible?) timing issue i am seeing. When the alert executes i see '0' however i know that after the injection jQuery is indeed loaded in the iframe (, i know that by executing the similar alert on click of a button in the iframe which correctly outputs '1').

let str = `<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script type="text/javascript">try{ alert(window.jQuery?'1':'0') } catch(e){ }</script>`

this.convertToNodes(str).forEach(eL => {
  iframe.contentDocument.head.appendChild(eL);
});

Anyway i can correctly inject the alert so that i see '1'?

0

There are 0 best solutions below