Working with Pop Ups and Browser Windows in JavaScript
This series of articles mainly concentrates on working with browser windows and sub-windows in JavaScript. You can reuse these scripts to inject into server side controls easily (especially in .NET and Java).
Working with Pop Ups and Browser Windows in JavaScript - How to access the data available in a pop up window from within the main window using JavaScript (Page 3 of 5 )
Now, let us try to develop a simple script (JavaScript) which writes user specified information to a pop up window dynamically. We shall even access the information available in pop up window from within the main window! Have a look at the following code:
<html> <head> <meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5"> <script id="clientEventHandlersJS" language="javascript"> <!-- // global variable for subwindow reference var w; function openNewWindow( ) { if (!w || w.closed) { w = window.open("","NewWindow","status,height=200,width=300"); // delay writing until window exists in IE/Windows setTimeout("openNewWindow( )", 50); } elseif (w.focus) { // window is already open and focusable, so bring it to the front w.focus( ); } // assemble content for new window var c = document.all.txtHTML.value; // write HTML to new window document w.document.write(c); w.document.close( ); // close layout stream }
function Button1_onclick() { openNewWindow(); }
function Button2_onclick() { alert(w.document.getElementById("txtName").value ); }
This is another interesting concept pertaining to the use of pop up windows. We will certainly know about retrieving information available on the current web page. But, how about accessing the information available in a pop up window from within the main window? This section does the same.
The following is the function I used to achieve this:
I am expecting that the pop up window contains a text box named "txtName" and I am trying to access the information available in it. To access the information in a pop up window from within the main window, we need to work with the "document" object of the pop up window. Once you get the "document" object, the rest of the routines will be very similar to the ordinary way we implement in main windows.