title: Javascript: Can't access iframe document
body: <p>I'm just trying to pull an image from a site (not necessarily Bing but the problem is with every site it seems). <br>Running this code it seems to have failed at the 'if' statement. This makes me think there is no document. <br>
  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.</p>
  
  <p>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.</p>
  
  <p>HTML:<br>
      <code>&lt;iframe id="test" src="https://www.bing.com/images/search?q=test"&gt;&lt;/iframe&gt;
  &lt;button onclick="myFunciton()"&gt;Go&lt;/button&gt;</code></p>
  
  <p>Javascript:<br></p>
  
  <pre><code>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;
  }
  </code></pre>