Pass Variable to a Popup Window using Javascript -
i need pass text current page popup window without going server hit. information (herewith represented 90) available in parent form (it's paragraph-long text stored in hidden variable). need display popup.
here's i've tried, works extent doesn't work if pass text, instead of number. second concern solution kinda looks ugly. tips? thank you.
this scce, can run straight in machine.
<html> <head> <title>a new window</title> <script type="text/javascript"> var newwindow; var data; function makenewwindow(param) { data = param; if (!newwindow || newwindow.closed) { newwindow = window.open("","sub","status,height=200,width=300"); settimeout("writetowindow()", 50); /* wait bit give time window created */ } else if (newwindow.focus) { newwindow.focus( ); /* means window open*/ } } function writetowindow() { var k = data; alert(data); var newcontent = "<html><head><title>additional info</title></head>"; newcontent += "<body><h1>some additional info</h1>"; newcontent += "<scr"+"ipt type='text/javascript' language='javascript'> var localvar; localvar = "+ k +"; document.write('localvar value: '+localvar);</scr"+"ipt>"; newcontent += "</body></html>"; // write html new window document newwindow.document.write(newcontent); newwindow.document.close( ); // close layout stream } </script> </head> <body> <form> <input type="button" value="create new window" onclick="makenewwindow('90');" /> </form> </body> </html> actually, googled , saw other approach uses window.opener.document.forms.element, here, window has know in advance has read parent. need able pass vary:
<textarea rows="15" name="projectcontent" id="projectcontent" cols="87"></textarea> <a href="javascript:;" onclick="window.open('viewcon.asp', 'my_new_window','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=yes, width=625, height=400');"><b>view content</b></a> <head> <title>view project content</title> </head> <body> <img src="/images/toplogo.jpg"><br/> <script language="javascript"> document.write(window.opener.document.forms['yourformname'].elements['projectcontent'].value) </script> <img src="/images/bottomlogo.jpg"> </body> </html>
use window.opener
from mozilla developer network: when window opened window, maintains reference first window window.opener. if current window has no opener, method returns null.
https://developer.mozilla.org/en-us/docs/web/api/window.opener
this way can have on original window callback, , can notify window it's load , ready, rather wait random delay...
you add function on original window:
window.popupready = function (callbacktopopup) { callbacktopopup(newdata); } then popup can tell parent window it's ready , pass callback update data..
and on popup try like:
window.dataready(newdata) { alert(newdata); } document.addeventlistener("load", function() { window.opener.popupready (dataready); } i didn't test code, take such path should ensure popupwindow ready , along spirit of javascript.
Comments
Post a Comment