Search code examples
javascriptiframe

Can you target an element in an iFrame from the same site?


What I'm trying to do is to is to open the minicart url in an iframe on a page so i can get the basket total from the cart. i've been able to do this, but when i try to target what's on the iframe i'm unable to do so. On the site when i console.log "pre" i do see that in the html collection the innerHTML does display what is outputted. Any idea how or if i would be able to target this?

var iframevar = document.getElementById('freeGiftiFrame');
var elmnt = iframevar.contentWindow.document.getElementsByTagName("pre");
console.log(elmnt)
<iframe id="freeGiftiFrame" class="cartPopout" name="iframe_a" height="100px" width="100%">
  <html>
  <head></head>
    <body>
      <pre style="word-wrap: break-word; white-space: pre-wrap;">
      {"miniCartCount": 1, "miniCartPrice": "€&nbsp;30,00"}
      </pre>
    </body>
  </html>
</iframe>


Solution

  • For anyone with the issue adding in = sandbox="allow-same-origin allow-scripts" does the trick for me