I'm currently facing an issue when trying to fetch an image from a website (not specifically Bing, this problem arises with every site).
Upon running the code, it seems to be failing at the 'if' statement, indicating that there might not be a valid document.
With my limited understanding of HTML and Javascript, I am struggling to identify the root cause of this failure. Any assistance in resolving this would be greatly appreciated.
Although I usually wouldn't require multiple variables for such a task, I created this code in order to pinpoint the issue. I have also attempted using 'window.frames[0].document' without success.
HTML:
<iframe id="test" src="https://www.bing.com/images/search?q=test"></iframe>
<button onclick="myFunction()">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;
}