web space | free website | Business Web Hosting | Free Website Submission | shopping cart | php hosting
Free Web tutorials covering HTML, CSS, JavaScript, and DHTML from beginner to advanced. Free downloads and developer resources. Personalized help via email, form, and chat. free, web, tutorials, HTML, html, CSS, css, stylesheet, cascading stylesheet, Javascript, javascript, JavaScript, DHTML, dhtml, beginner, advanced, web development, web page, web site, free web tutorial, free HTML tutorial, free CSS tutorial, free css tutorial, free cascading stylesheet tutorial, free stylesheet tutorial, free javascript tutorial, free DHTML tutorial, free HTML class, free CSS class, free stylesheet class, free cascading stylesheet class, free javascript class, free DHTML class

Home <Code_Punk>'s Web Tutorials
Beginning HTML
Beginning CSS
Advanced HTML
Advanced CSS
Beginning JavaScript
Advanced JavaScript
DHTML
Website Development and Management
Downloads & Resources
HELP!!
Forum
Participate
E-Mail <Code Punk>

JavaScript Lesson 9:
Opening & Closing Windows


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 anything more than the <a> tag's TARGET="_blank" attribute right now, but we'll 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 set "lovechild" equal to the "window.open()" statement without using a VAR statement before "lovechild":

<script language="JavaScript">

function open_a_window_please(){
lovechild = window.open("childpage.htm");

</script>

It's very important to notice that we do not use the JavaScript "var" to declare this name as a variable (var lovechild =). That's because "lovechild" is a name, not a variable.

The difference is that a variable is set up to hold input data like in our Confirm() and Prompt() boxes. A name, on the other hand, assigns a string, like "lovechild", to a JavaScript object, like our child window.

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:

OPEN

CLOSE



Nifty, huh? What's important to remember here is that a name assignment isn't a variable (VAR) assignment. A name must be given to a created object if you want to refer to that specific object later with JavaScript.

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. Don't worry so much about this now. It's not required and not used by JavaScript, but HTML. Most programmers use the same name both in the JavaScript assignment and the HTML name in the 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 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 assigned to it to distinguish it from the others. Sort of like what your parents did to you. Try a few self-closing JavaScripts in the child windows' pages -- self.close(), window.close().



Continue To Lesson 10 -- Sizing & Styling Your Window >>>

<<< Back To Beginning JavaScript Index




<Code_Punk>'s Web Tutorials