Access elements of a child window from a parent window using Javascript

I need to access the elements of a child window from a parent window. I wrote sample fragments below.

Parent HTML:

<html> <head> <title>Parent</title> <style> div{ float:left; cursor:pointer; } </style> <script type="text/javascript"> var SubpopUpWin=""; function Opennew(passedURL){ SubpopUpWin = window.open("popups.html", '_blank','toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes'); SubpopUpWin.document.getElementById("ifrm").src=passedURL; SubpopUpWin.document.getElementById("ifrm_title").innerHTML=passedURL; } </script> </head> <body> <div onclick="Opennew('http://www.google.com')">Google</div> <div onclick="Opennew('http://www.yahoo.com')">Yahoo</div> <div onclick="Opennew('http://www.bing.com')">Bing</div> </body> </html> 

popups.html

 <html> <head> <title>Child</title> <style> div{ float:left; } </style> </head> <body> <div> <div id="ifrm_title"></div> <div style="margin-top:20px"> <iframe id="ifrm" src="" width="470" height="270" frameborder="0" style="margin-top: 34px" scrolling="no"></iframe> </div> </div> </body> </html> 

The above code does not work. Even I used below script.

 <script type="text/javascript"> var SubpopUpWin=""; function Opennew(passedURL){ SubpopUpWin = window.open("popups.html", '_blank','toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes'); SubpopUpWin.onload=function(){ SubpopUpWin.document.getElementById("ifrm").src=passedURL; SubpopUpWin.document.getElementById("ifrm_title").innerHTML=passedURL; } } </script> 

The above code also does not work. Please share your suggestions / solutions ...

thanks

+4
source share
1 answer

I feel this is due to some security. Instead, you can try:

 <html> <head> <title>Parent</title> <style> div { float: left; cursor: pointer; } </style> <script type="text/javascript"> var SubpopUpWin = ""; var testUrl = ""; function Opennew(passedURL){ testUrl = passedURL; SubpopUpWin = window.open("popups.htm", '_blank', 'toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes'); } </script> </head> <body> <div onclick="Opennew('http://www.google.com')"> Google </div> <div onclick="Opennew('http://www.yahoo.com')"> Yahoo </div> <div onclick="Opennew('http://www.bing.com')"> Bing </div> </body> 

 <html> <head> <title>Child</title> <style> div { float: left; } </style> </head> <body> <div> <div id="ifrm_title"> </div> <div style="margin-top:20px"> <iframe id="ifrm" src="" width="470" height="270" frameborder="0" style="margin-top: 34px" scrolling="no"> </iframe> <script type="text/javascript"> document.getElementById("ifrm").src = window.opener.testUrl; </script> </div> </div> </body> 

+7
source

Source: https://habr.com/ru/post/1400206/


All Articles