0

I have a simple Google Apps Script web that reloads page. I want the page after reload stay inside the iFrame. When I was preparing question for SO I was getting strange error Uncaught TypeError: Cannot set property 'src' of null when

var iFrame = document.getElementById('myIframe')
var iFrame2 = $("#myIframe")
var test = document.getElementById('test')
console.log(iFrame)
console.log(iFrame2)
console.log(test) 

none of the above elements were found - myIframe nor test. The source of the page is

<iframe id="myIframe" src="https://script.google.com/macros/s/AKfycbyraOI6MBYVeZ9CJlo79lR-OtVxyfMcU962wRG3iO0seu5LwzQMws-1iSm7KuRCB9S6rA/exec" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;height:85%;width:100%;position:absolute;top:505x;left:0px;right:0px;bottom:0px;"></iframe>

<div id="test"></div>

You can check here http://www.automatt.cz/iframe-reload/ The console once you type passcode returns

enter image description here

So, the javascript is running and is inside the iFrame page.

Strange is that on my different free google account almost the same code works. Could someone help me and explain what is happening?

Radek
  • 13,813
  • 52
  • 161
  • 255
  • is the javascript before or after the iframe? – Jaromanda X Jun 16 '21 at 09:22
  • The javascript is inside the iFrame. This works in other of my project. – Radek Jun 16 '21 at 09:25
  • If the code is within the `iframe`, then you're asking the browser to find the objects **within** the `iframe`. It cannot find objects outside of the current document unless you specifically tell it to by using something like `window.parent` – freefaller Jun 16 '21 at 09:27
  • hang on ... the `var iFrame = document.getElementById('myIframe')` is INSIDE the content of `iframe id="myIframe"`? couldn't possibly work ... the javascript inside the iframe can't access the containing document like that – Jaromanda X Jun 16 '21 at 09:27
  • `This works in other of my project` impossible - unless the iframe is the same domain as the parent, *and* you are using `parent.querySelector` – Jaromanda X Jun 16 '21 at 09:34
  • @JaromandaX you're right, it does not work in my other project. So I should use parent.getElementById() ? do you want to create an answer? – Radek Jun 16 '21 at 09:51
  • No, because it won't work cross domain – Jaromanda X Jun 16 '21 at 09:51
  • ops. so there is no solution to it? – Radek Jun 16 '21 at 09:53
  • Try with this solution. https://stackoverflow.com/questions/17237142/cannot-set-property-src-of-null – Slobodan Draksimovic Jun 16 '21 at 10:10
  • 1
    If you want to change the src value of an iframe from inside it you can use - window.location . If you want to access another iframe(sibling iframe) your frame should be in the same domain as your parent as mentioned. – Tushar Shahi Jun 16 '21 at 10:28

0 Answers0