0

I am a javascript beginner . I wrote a javascript code to open a website in new window and then get the content of that new window and display it in the first window .. and this is the code :

    <!DOCTYPE html>
<html><head></head><body>
<div id="display"></div>
<script type="text/javascript">
var newwin=window.open("http://www.w3schools.com");
newwin.onload=function () 
{display.innerHTML = newwin.document.documentElement.innerHTML;};
</script>
</body></html>

It didn't work .. the web site opened in the new window but the content didn't appear in the first window .. why? thanks in advance.

shady
  • 33
  • 2
  • 1
    What do you want to do? Have two windows with same content? – Arg0n Nov 28 '15 at 17:38
  • Why don't you use an iframe? – Ram Nov 28 '15 at 17:40
  • You will have the same problem with an iframe unless the iframe is from the same domain http://stackoverflow.com/questions/1451208/access-iframe-elements-in-javascript – bhspencer Nov 28 '15 at 17:42
  • @bhspencer I know how Same-origin policy works. The point was using an iframe for showing another page. Of course you can't read the content of that page using JavaScript for security reasons. Also see https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage – Ram Nov 28 '15 at 17:44
  • 2
    The OP is trying to access www.w3schools.com in there example. Presumable that is not in the same domain – bhspencer Nov 28 '15 at 17:46
  • If [`X-Frame-Options`](https://developer.mozilla.org/en-US/docs/Web/HTTP/X-Frame-Options) header of another domain allows embedding of the page you can use an iframe. But you can't still use JavaScript for manipulating that page if some other options haven't been set. – Ram Nov 28 '15 at 17:55
  • Still, if OPs only purpose is to show w3schools somewhere on his page, why would not an `iframe` work? Where in the question do he say that he needs to access the document? – Arg0n Nov 28 '15 at 17:59
  • @Arg0n Yes, he can. But for using an iframe there are some prerequisites that should exist. Modern browsers are more stricter that before. OP tries to read the `innerHTML` of another page's `document` element and that indicates that he wants to _access_ the document. Same-origian policy by default doesn't allow this. – Ram Nov 28 '15 at 18:03
  • @Vohuman Well, the only thing he does is copying the entire document into his page, leading to believe the only thing he want to do is show it. – Arg0n Nov 28 '15 at 18:05
  • @Arg0n That's true. That's why I have suggested using an iframe. What I wanted to say is he/she can't read/manipulate the contents of the embedded page by using JavaScript. – Ram Nov 28 '15 at 18:09

3 Answers3

0

You cannot do this if the child window is from a different domain. Each domain is sandboxed from the others for security reasons.

bhspencer
  • 13,086
  • 5
  • 35
  • 44
0

Use _parent as a second parameter to position your webpage. (BTW I am assuming you are allowing popups in your browser settings)

var newwin=window.open("http://www.w3schools.com", "_parent");

swapnesh
  • 26,318
  • 22
  • 94
  • 126
0

There are 2 errors that prevent you:

  1. Different domains
  2. Even if same domain, you set onload function after calling window.open(). So the new "onload" behavior never runs

Solution for same domain:

<!DOCTYPE html>
<html><head></head><body>
<div id="display"></div>
<script type="text/javascript">
var w=window.open("another.html");
var tid = setInterval function () {
    if (w.document.readyState !== 'complete')
    return;
    clearInterval(tid);  
    display.innerHTML = w.document.documentElement.innerHTML);  
}, 100);
</script>
</body></html>
yelliver
  • 5,648
  • 5
  • 34
  • 65