0

I'm trying to embed the interactive radar in an iframe, then click a certain button inside the iframe which toggles full-screen mode. The button has the title "Toggle Full Screen Mode," so the javascript should search for and find that button, then click it; but it doesn't work and the Safari console says the following:

TypeError: undefined is not an object (evaluating 'document.getElementById('screen2').contentDocument.querySelectorAll("button[title='Toggle Full Screen Mode']")[0].click')

I'm basing my code off of this answer.

Is this possible to do, and if so what am I doing wrong? If it is not possible, then what workarounds can I use to accomplish the same thing?

Here is the code for the button inside the iframe.

<button title="Toggle Full Screen Mode" aria-label="toggle fullscreen"><span classname="icon icon-font iconset-map icon-maximize" class="icon icon-font iconset-map icon-maximize"></span></button>

And here is my code:

document.getElementById('screen2').contentDocument.querySelectorAll("button[title='Toggle Full Screen Mode']")[0].click();
<iframe style="height:500px; width:800px;" id="screen2" src="https://weather.com/weather/radar/interactive/l/d1936f8be052152442dd64abf7364f3650c8b2d6ad905be1cacef7ad56eced11?layer=radar&animation=true&zoom=9"></iframe>
htmlcat
  • 336
  • 3
  • 10
  • 3
    I'm gonna go out on a limb and say that you're not hosting this page on `weather.com`? You're running into a security feature built into all browsers: the [same-origin policy](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy) of iframes. You won't be able to access anything in the contentDocument of an iframe unless your iframe contents comes from the same domain as your page. – David784 May 26 '19 at 19:58
  • Correct. What workaround would I use to make this work? – htmlcat May 26 '19 at 20:08
  • Put the button below the Iframe and then resize the Iframe to 100% on click or similar. Problem solved. You can't capture events or manipulate dom elements on another domain, forget it. – Codebeat May 26 '19 at 23:09

0 Answers0