I'm just trying to pull an image from a site (not necessarily Bing but the problem is with every site it seems).
Running this code it seems to have failed at the 'if' statement. This makes me think there is no document.
I've been trying to figure out why this won't work but with my limited HTML/Javascript knowledge I just can't. Can someone enlighten me please.
I wouldn't need this many variables normally but in order to figure out the problem I made this code. I've also tried window.frames[0].document with no luck.
HTML:
<iframe id="test" src="https://www.bing.com/images/search?q=test"></iframe>
<button onclick="myFunciton()">Go</button>
Javascript:
function myFunction() {
var x = document.getElementById("test");
var y = (x.contentWindow || x.contentDocument);
if (y.document){y = y.document;}
var test = y.getElementsByTagName("img")[0].src;
}