I can always tell when there is a new command that is in vogue. I start getting all kinds of email asking how to do it. Well, this is the new thing. I don’t know why, but everyone wants to know how to create these little windows that pop up. Some sites even use them as control panels operating the main window.
If you decide to go with this kind of pop up window, use it sparingly. I think they become an annoyance after a while. Sort of like some guy telling you the same joke every time he meets you. It’s fun about three times. Then you start yelling at the screen. I do at least. It really scares the cat.
But if you insist on doing one, let’s do it right! We’ll start at the beginning.
The Basic JavaScript Format
Oh yes! This is done through JavaScript. But it’s a very simple JavaScript, so don’t get worried so soon. Here’s the basic format.
window.open (‘page.html’) –> |
|---|
Heck, you can almost guess what each of the parts does. Since this is a script, you need the opening SCRIPT LANGUAGE=”javascript” statement, and of course the /SCRIPT statement at the end. The are used to hide the text from older browsers that don’t read JavaScript. You see, if you didn’t have those in there, the text would display. This way it doesn’t.
…but it’s the stuff in the middle that that makes the magic.
window.open (‘page.html’)does just what it says. It opens a window (a new browser screen actually) and fills it with the page within the parentheses and single quotes. In the example above the HTML document that fills the window is called page.html.
Is that it?
Technically yes. That little script will work and open a new browser window and you’ll get the effect. But wouldn’t it be great if we actually had the ability to configure the window any way we wanted? You bet. Here’s how.
Configuring The Window
Now we get to the commands I used on this page to get the little window effect. This is exactly what I have:
window.open (‘titlepage.html’, ‘newwindow’, config=’height=100,
width=400, toolbar=no, menubar=no, scrollbars=no, resizable=no,
location=no, directories=no, status=no’)
–>
(Get all on one line…no spaces)
Here’s What’s Happening
- starts the JavaScript.
- window.openis the JS command to open a new browser window.
- ‘titlepage.html’is the name of the page that will fill the window.
- ‘newwindow’is the name of the window. This you need. We are going to use commands intended to alter the window the script is opening. In order for the JavaScript to know what item it is dealing with, the item has to have a name. So we give it one. I went with newwindow. But it could have just as easily been zork, or woohaa, or raspberry.
- config=denotes that what follows configures the window. (This command really isn’t required, but it’s a good idea to use just to keep things straight)
- height=100denotes the height of the window in pixels.
- width=400denotes the width of the window in pixels.
- toolbar=no denotes if there will be a toolbar on the newly opened window. Set this to yes if you want one – no if you don’t.
The toolbar is the line of buttons at the top of the browser window that contains the BACK, FORWARD, STOP, RELOAD, etc. - menubar=no denotes if there will be a menubar. Set this to yes if you want one – no if you don’t.
The menubar is the line of items above labeled FILE, EDIT, VIEW, GO, etc. - scrollbars=nodenotes if there will be scrollbars or not. Ditto with the yes and no deal. I wouldn’t make a new window that would need scrollbars anyway. I think it kills the effect.
- resizable=nodenotes if the user can change the size of the window by dragging or not.
- location=no denotes if there will be a location bar on the newly opened window. Use yes and no again.
The location bar is the space at the top of the browser window where the page URL is displayed. - directories=no denotes if there will be a directories bar on the new window. Use yes and no.
This is the bar at the top of the browser window that has the bookmarks and such. - status=no denotes if there will be a status bar. Use yes and no.
The status bar is the area at the very bottom of the browser screen that tells you “Document Done”. - ends the whole deal.
Can I Lose the Title Bar?
The answer use to be no, but there is now a script that will allow it in IE 5.0 and above browsers. I have a tutoral on it here.
Where Do I Place The Code?
Anywhere on the page. Towards the top will ensure it runs sooner in the process than if you put it at the end. It’s a pretty robust script that doesn’t need a special placement.
Page Inside A Page
The example I gave above relies on two pages. The first is the main HTML Document that carries the JavaScript. The second is the HTML Document that is displayed inside the new window.
Here I’ll get a little more fancy with the JavaScript code. I’ll show you how to set it up so that the new window is included inside the main HTML Document completely. One page – two windows.
function openindex() OpenWindow.document.write(“”) OpenWindow.document.write(“ Hello!”)OpenWindow.document.write(“This text will appear in the window!”) OpenWindow.document.write(“”) OpenWindow.document.write(“ |
|---|