Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Controlling Browser Properties With JavaScript
Get to grips with the Window, Location and History objects.

| Starting At The Top |

I'll begin at the beginning, with the top-level Window object. The Window object is at the top of the DOM hierarchy; all other nodes are under it, including objects representing the document's frames, forms, images, links and styles. This object comes with a bunch of useful methods, of which the two most commonly-used ones are the open() and close() methods.

Here's an example of using the Window object's open() method to open a new browser window:


<script language="JavaScript">
// open a new window
window.open("http://localhost", "new");
</script>


As you will see when you run it in a browser, this line of code opens up a new window and loads the URL "http://localhost" into it. Thus, the first argument to window.open() is always the URL to be loaded, while the second is the window name.

You can also control the appearance of the window, by adding a third argument - a comma-separated list of window attributes. Here's an example:


<script language="JavaScript">
// open a new window
window.open("http://localhost", "new", "toolbar=no,status=no,menubar=no,scrollbars=no");
</script>


Here's a brief list of the important attributes that window.open() lets you control:

"width" - controls the width of the new window

"height" - controls the height of the new window

"location" - toggles the address or location box

"toolbar" - toggles the toolbar

"status" - toggles the status bar

"menubar" - toggles the menu bar

"scrollbars" - toggles the scrollbars

"directories" - toggles the directories or links bar

"resizable" - is the window resizable ?

These attributes can be turned on or off by equating them with either a 1 or 0. Height and width values must be specified in pixels.

Thus, if you wanted to open a window with the address bar and status bar off, but the links bar on, you would use this code:


<script language="JavaScript">
// open a new window
window.open("http://localhost", "new", "location=no,status=no,directories=yes");
</script>


while if you wanted a window of height 640 and width 480, non-resizable and with no bars showing, you could use this code:


<script language="JavaScript">
// open a new window
window.open("http://localhost", "new", "width=480,height=640,toolbar=no,resizable=no,menubar=no,location=no,status=no,directories=no");
</script>


Note that leaving the third argument to window.open() empty pops open a window with default settings. Note also that you should not leave spaces between the various window attributes listed in the third argument to window.open() - they can cause problems with certain browsers.

Now, how about closing a window that you've opened? It's pretty easy - just use the window.close() method, as in the following example:


<a href="javascript:window.close()">Close Window</a>


Now, when the user clicks the "Close Window" link above, the window should close automatically. Note that if the window you're trying to close through JavaScript is the primary browser window, browser security settings might require you to confirm your action through a dialog box.


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback