![]() |
||||||||||
|
||||||||||
|
|
JavaScript: Popup WindowsThis how-to article about JavaScript popup windows shows you how to create a basic popup window as well as several more complex popups and includes demos and source code plus a reference list of options for popups. JavaScript allows you to create (open) new windows. Run whatever HTML code you wish in these windows. You can even have actions in one window effect another window. Example 1: Create a Basic Popup Window The statement used to open basic popup for Example 1 is: window.open("win1.html","Window1",
"menubar=no,width=430,height=360,toolbar=no"); In the above example, the syntax is to first specify the web page to load into
the window. Next, set the title of the window. Last, specify the options for the window. Note:
the options are all in a single set of quotes.
<a href="javascript:self.close()">close window</a>
Options for JavaScript Popups
Example 2: Interacting With Two Windows win2 = window.open("win2.html", "Window2",
"width=310,height=600,scrollbars=yes");
Click Here to Open Window 2. The contents of the popup window provide more explanation. If you opened Window 2, and read it, you should have returned to this page via the focus change. If you didn't, what are you waiting for? The line of code to change focus is: <a href="javascript:opener.focus()">change focus</a>
We can even close Window 2 from this link using the following statement: <a href="javascript:(win2.close()">close</a>
Example 3: Writing To a Popup Window After It Is Open
// open the window
win3 = window.open("", "Window3", "width=320,height=210,scrollbars=yes"); // write to window win3.document.writeln("<h2>This is written to Window 3 by the main window</h2>");
Example 4: Using a Popup for Site Navigation When using links in an opened window, you need to reference the 'opener' window to have links opened in that window. Do this by writing a function in the opened window which points links to the opener window. This is best shown with an example:
function openURL(sURL) {
opener.document.location = sURL; } <a href="javascript:openURL('home.html')">Go Home & </a> Click here for a Navigation Window Example
|
| © 1997-2008. astonishinc.com All Rights Reserved. |