School Map:
Home / Enroll
$ Student Records; Class
$ News @ LIVE
Tech Radio * Support/FAQ
| Store | FORUMS
Script School Course #7 - JavaScript - Week #3 |
Printable Version of this page HERE |
JavaScript gives You the ability to create new browser windows with Your scripts. You can control which window elements appear - the status bar, tool bar, menu bar, location bar, scroll bar, and if it is resizable... You can also make a custom sized window, position it on the screen, and place it above or below the current window. The contents can be pre written HTML documents or they can be written 'on the fly'. You can also rewrite the 'parent' window document from another window - for example, a 'Table of Contents' pop-up. ( However, with the popularity of 'pop-up blockers', if this is the only way to navigate Your site, it might be a good idea to let Your users know that they'll have to allow pop-ups and JavaScript if they want to see Your site! )
To open a new window, use the open() window.open() open()
var reference = window.open("url", "name", "elements");
window.open()
"http://www.mysite.com/book1/chapter1.html"
or a relative link: "chapter1.html"
If url is blank and name isn't being used for a currently
existing browser window, then var reference = window.open("","name");
var mywindow = window.open("http://www.mysite.com/new_window.htm","NewWindow",
"width=400,height=350,scrollbars=yes,toolbar=no,status=yes,resizable=yes,menubar=no,location=no");
The line of code above can be placed in a function within <SCRIPT> tabs in the <HEAD> to open a new window:
function openMyWindow()
{
var mywindow = window.open("new_window.htm","NewWindow",
"width=400,height=350,scrollbars=yes,toolbar=no,status=yes,resizable=yes,menubar=no,location=no");
}
Which You could call from an onClick event handler:
<FORM>
<INPUT TYPE="button" VALUE="Open Window" onClick="openMyWindow()"
style="color:#0000FF; background-color:#EEEEFF">
</FORM>
Which gives us: ( live example - try it )
To place a window where You want it on the screen, set the aspects left
and top in pixels, in the elements list argument in
the open() screen.height screen.width screen.availHeight screen.availWidth + operator... see Week 1 of this class.
// set Global variable: positionedwindow to null var positionedwindow = null; function footnote()
{
var toploc = screen.height - 130;
var winwidth = screen.width - 10;
var positionedwindow = window.open("footnote.html", "foot",
"width="+ winwidth +",height=100,scrollbars=yes,top="+ toploc +",left=0"); positionedwindow.focus();
}
To close a window, use the close() method of the window object.
If You want to close a window from itself, the statements: window.close() self.close()
<a href="javascript:self.close()" onMouseOver="window.status='Close Window'; return true;" onMouseOut="window.status=''; return true;">Close Window</a>
Or, for a button:
<FORM>
<INPUT TYPE="button" VALUE="Close Window" onClick="window.close()"
style="color:#FF0000; background-color:#FFEEEE">
</FORM>
Well, we don't want to close this window, so let's try closing the footnote example - from this window. By using the reference variable created when the window was opened, You can close another window from the current one. Since the reference in the last example, Footnotes, was: positionedwindow it is used in the onClick Event Handler:
<FORM>
<INPUT TYPE="button" VALUE="Close Window" onClick="positionedwindow.close()"
style="color:#FF0000; background-color:#FFEEEE">
</FORM>
Creating: (live example - click the Footnotes button to open the window again first)
Actually, for this function to work properly, we declare the reference
variable as a Global variable. var positionedwindow =
null; return
the reference from the function as we create a Global Scope variable
- if the function is called in the main script - not an Event Handler. Otherwise,
if a Global isn't created, the reference is lost when the run of the
function is done. Of course we could also reestablish a reference
from the name as You will see in the Navigator example in just a moment.
We also use the window.focus()
So the script containing our function to open the window changes to:
var mywindow = "";
function openMyWindow()
{
mywindow = window.open("new_window.htm","NewWindow",
"width=400,height=350,scrollbars=yes,toolbar=no,status=yes,resizable=yes,menubar=no,location=no");
mywindow.focus();
}
To close a window created by a script in another page, get a new reference
using the window's name with an empty string "", for the
url argument in a window.open()
var newreference = window.open("","WindowName");
newreference.close();
A window that creates another is called the 'parent' of the new window. The
new window is known as the 'child'. We have already seen how a window can refer
to it's own children or the children of other windows by using the reference
or perhaps after getting a new reference using a name.
Any open window can be referred to this way. JavaScript also has the property
opener closed The code below will reopen
the parent window if it has been closed:
if (window.opener.closed)
{
var newparent = window.open("parentURL.htm", "parentwindow");
}
When You are fully done with a window, set the opener property to keyword:
null so JavaScript can free up memory:
window.opener = null;
We first saw the write() method of the document object in Week 1. The following example uses the similar writeln() method of the document object. The only difference is that writeln() causes the next write() or writeln() to start on the next line. We are writing to the document, as though we were writing an HTML page in an editor. The document object is then interpreted by the browser window. The script rewrites to the same window it created each time a new Item is clicked, changing the TITLE bar, the text in the window, and the color of the background of the window. It rewrites to the same window, instead of recreating a new one, since the window already exists. We could have just as easily changed pictures or CSS settings. Included are links to open and close the parent Navigator window from it's child. Take a look at the example and the script for the first child we open from this page, which is annotated with comments:
Within the script example for the Navigator we open a window under all the
other open windows using the window method: window.blur()
if (updn == "dn")
{
subwindow.blur();
} else {
subwindow.focus();
}
Write a script that:
Hints:
For 2 & 3: Write to a variable through onClick Event Handler.
For 3: See Week 2 of this class for a live clock example.
Subtract a second each loop instead of adding one.
Set the date object with a variable.
Need help with your assignment or have questions about this week's course?
About The Instructors
TDavid is co-owner, programmer and webmaster for several sites devoted to programming including his own http://www.tdscripts.com/ He has done custom programming in various programming languages for companies all over the world. Every Friday at 2pm PST you can catch his weekly radio show dedicated to the technical side of webmastering and programming at http://www.scriptschool.com/radio/
LogSpirit is a student of Script School, the co-author of this course and available for hire for custom programming.
| Printable Version of this page HERE |
School Map:
Home / Enroll
$ Student Records; Class
$ News @ LIVE
Tech Radio * Support/FAQ
| Store
Advertising
| Link To Us | Privacy
Copyright 2000-2002 Script School Productions / KMR
Enterprises
No part of this website may be reproduced, copied and/or distributed
in any medium
without express written permission