Easy Java Script Windows
By Stephen Bucaro
Opening a browser window with JavaScript is extremely easy. If you don't believe me,
type this text into your web page.
<script>open()</script>
When you open your web page, a popup window named about:blank will appear.
In the code above we have used the fact that the browser defaults to Java Script.
Proper coding would have specified the script language and assigned the window to
a variable so that we could use that variable to control the window after we create
it. The example below shows the proper code to create a window and load a web page
named mypage.htm into the window.
<script language="JavaScript">
var myPopUp = window.open("mypage.htm","pop_window");
</script>
Of course, you don't have to load a local web page into the window, you can put
any URL in the first argument of the window.open function. Note the second argument
of the window.open function, "pop_window". This gives the window a name that you
can use to load a different page into the window, as shown below.
<a href="mypage2.htm" target="pop_window">Link Text</a>
You can add a third argument, called the "features list", to the window.open function.
Below is a list of the features that you can specify.
left | location of the left border of the window in pixels from the left side of the screen |
top | location of the top border of the window in pixels from the top of the screen |
width | width of the window in pixels |
height | height of the window in pixels |
menubar | specifies the menu bar |
toolbar | specifies the tool bar |
location | specifies the address bar |
scrollbars | specifies scrollbars |
status | specifies the status bar |
resize | specifies whether the window can be resized by the user |
Assigning a feature the value 1 provides the feature. Assigning a feature the value 0
prevents the feature. The example below creates a window with a menu bar and address bar,
but no tool bar. Note that the feature specifications are separated by commas, and you
can put them in any order.
<script language="JavaScript">
var myPopUp = window.open("mypage.htm", "pop_window",
"menubar=1, location=1, toolbar=0");
</script>
What happens if you omit the specification of a feature? That depends on the browser
version used to view the page. In some browsers, features not specified appear by default.
In some browsers features not specified are not provided by default. To get reliable
results, you should always specify all the features.
If you like to reuse code, you can specify the features list in a string variable
and then use that variable name as the features list argument in the window.open
function. This will prevent you from having to memorize the features list.
<script language="JavaScript">
var features = "left=100, top=100, width=256,
height=256, menubar=1, toolbar=0, location=0,
scrollbars=0, status=0";
var myPopUp = window.open("mypage.htm",
"pop_window",features);
</script>
|