[Home]Open a page in a new browser window

HomePage | RecentChanges | Preferences | Newbie Help

Category HTML FAQ -:- [Category JavaScript FAQ]?

There are two main ways of opening a page in a new window from a link.

The HTML way involves the <a href> tag using the target attribute. The value of the target attribute can either be a browser-reserved window name, or an alphanumeric name.

The main browser-reserved name in use is _blank, our example:

<a href="mynewpage.html" target="_blank">My New Page</a>

Will open mynewpage.html in a brand new window.

If you click this link again (while keeping the new window open), it opens another new window. And another. And another.

If we want to keep things cleaner by only opening a new window if one isn't already there then make up a window name (don't start the name with an underscore, else the browser thinks its one of its special windows) and use that:

<a href="mynewpage.html" target="thisismywindow">My New Page</a>

Now click our link lots of times, only one page should open.

Now how do we tell it how big the window should be? That is Javascript's job.

Javascript has the window.open() function that can create new windows. This function can take three parameters: window.open(URL,WindowName,WindowAttributes) . Have a read of Netscape's documentation: http://devedge.netscape.com/docs/manuals/js/client/jsref/window.htm#1202731 for full details on the window attributes that are possible. The two attributes we are really interested in are height and width

Now an example piece of code:

<script language="Javascript" type="text/javascript">
<!--
var newWindow;
function doInANewWindow(pageURL) {
  newWindow=window.open(pageURL, "newWin", "height=300,width=300");
}
// -->
</script>
<a href="myNewPage.html" 
   onclick="doInANewWindow('myNewPage.html'); return false;">See my new Page</a>

Voila.


See http://blooberry.com/indexdot/html/tagpages/target.htm for explanation of TARGET reserved values.
HomePage | RecentChanges | Preferences | Newbie Help
This page is read-only | View other revisions
Last edited May 26, 2003 12:04 am (diff)
Search:

This FAQ is happily hosted by Betadome Digital Media