The Open() Method
Opening a new browser window is pretty simple. You just use the "window.open()" method. Check out this this pop up window.
Where are the toolbar buttons? Notice that this window has been sized and styled far beyond anything you can do with the <a> tag's TARGET attribute. That's part of the beauty of using JavaScript to open windows.
Lets begin by making a link that will call the function that will open your window. If you don't use a function, the child window will open when the code is read as the parent page loads. This is how all those advertising pop ups generally get loaded. I'll be kinder and let you click a link when you're ready for a pop up window.
<body>
<a href="javascript: open_a_window_please()">Click here to open a window.</a>
The above tells the browser to run the function "open_a_window_please()" when the link is clicked. Now lets code the function in the <head> of our main page (soon to be parent page).
<script language="JavaScript">
function open_a_window_please(){
window.open("childpage.htm");}
</script>
The above function will open a new window and load the web page file named "childpage.htm". Of course you can open any page you want, even on other sites. Just put the URL to the page inside quotes.
This doesn't do anything more than the <a> tag's TARGET="_blank" attribute right now, but we are about to learn all sorts of things we can do with this new window.
Closing Your Window
The first thing we'll learn to do is close the window we just learned how to open. Before we can close the child window from the parent window, we need to give the child window a name that can be referred to in JavaScript.
I'm going to name our window "lovechild". To attach this name to the new window, we'll make a VAR "lovechild" equal to the "window.open()" statement:
<script language="JavaScript">
function open_a_window_please(){
lovechild = window.open("childpage.htm");
</script>
Now we have a name that can tell the browser which window to close. We're ready to use the "window.close()" method. In the parent page add the following closing link:
<body>
<a href="javascript: open_a_window_please()">Click here to open a window.</a>
<a href="javascript:lovechild.close()">Click here to close the child window.</a>
Try the code out by clicking the links below:
Your Window's Second Name
There's a second name your child window will have, too. It's put in the parentheses of the "window.open()" method:
lovechild = window.open ("childpage.htm","another_baby_window")
This second name, "another_baby_window", isn't used by JavaScript. It's used when the child window is referred to by an HTML tag like a form submission. It's the name used by the TARGET attribute of an <a> tag. Most programmers use the same name both in the JavaScript assignment and the HTML name in the parentheses.
Just remember that the name JavaScript uses is the VAR assignment and the name used by <a target="window name"> is the name in the "window.open()" parentheses.
My "Closing" Comments
One last note on closing. You can't close a window you didn't open. There are some convoluted schemes to simulate this effect, but most are just downright irritating. If you close the original window a viewer used to reach your site, they'll get a warning and can prevent the closing in all modern browsers.
You can close a window from a link inside itself (as opposed to a parent window) by using "window.close()" or "self.close()" as well as its window name. Again, if you didn't open the window, you can't close it.
Remember that if you want to manipulate a window with JavaScript, you need to open it with JavaScript and assign it a unique object name.
You probably noticed that I put "window.open()" in a function, but "window.close()" right in the HTML tag. As you'll soon see, your "window.open()" statement will get pretty bulky, and bulky JavaScript is best put in a function. The "window.close()" method doesn't get much bigger and is most frequently coded right in the HTML tag.
Exercises
Make a page that will open and close at least three child windows. Remember that each child window will need a unique name (var) assigned to it to distinguish it from the others. Sort of like what your parents did to you and any siblings. Try a few self-closing JavaScripts in the child windows' pages -- self.close(), window.close().
To Next Beginning JavaScript Lesson
Back To Beginning JavaScript Index
|