HTMLGoodies
The ultimate html resource
Earthweb.com


About the Double-Underlined Links



Search Clipart.com:



internet.commerce
Televisions
Home Improvement
Boat Donations
Condos For Sale
KVM over IP
Free Business Cards
Domain registration
Computer Deals
Remote Online Backup
Imprinted Gifts
Promotional Gifts
Logo Design
Computer Hardware
Memory

HTML Goodies : Beyond HTML : Javascript: So You Want To Open A Window, Huh?

Just because Web sites are easy to build these days, that doesn't mean it's easy to build a quality Web site that meets your business objectives.

Before developing your next Web site, or redesigning an existing site, download this Internet.com eBook to guide you through the process and plan your project, whether you're developing a site in-house or outsourcing the project.
Register now for your free Internet.com membership to download your complimentary eBook. Membership will also give you access to:

eBook library         Whitepapers         Webcasts
Newsletters         WinDrivers

HTML GOODIES TO GO NEWSLETTER


Other Related Newsletters

Serve your customers, not your servers, with VERIO FreeBSD VPS. Click here for your full-access, test-drive.

So You Want To Open A Window, Huh?


By Joe Burns

Use these to jump around or read it all...
[The Basic JavaScript Format]
[Configuring the Window]
[Page Inside A Page]
[Closing the Window]
[Other Fun With New Windows]
[A New Window Midi Trick]

     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. All the Geocities pages have them. Other pages 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.

<SCRIPT LANGUAGE="javascript">
<!--
window.open ('page.html')
-->
</SCRIPT>

     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 <!-- and 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:


<SCRIPT LANGUAGE="javascript">
<!--
window.open ('titlepage.html', 'newwindow', config='height=100,
width=400, toolbar=no, menubar=no, scrollbars=no, resizable=no,
location=no, directories=no, status=no')
-->
</SCRIPT>

(Get all on one line...no spaces)


Here's What's Happening

  • <SCRIPT LANGUAGE="javascript"> starts the JavaScript.
  • window.open is 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=100 denotes the height of the window in pixels.
  • width=400 denotes 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=no denotes 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=no denotes 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".
  • </SCRIPT> 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.


<SCRIPT LANGUAGE="JavaScript">

function openindex()
      {
OpenWindow=window.open("", "newwin", "height=250, width=250,toolbar=no,scrollbars="+scroll+",menubar=no");
OpenWindow.document.write("<TITLE>Title Goes Here</TITLE>")
OpenWindow.document.write("<BODY BGCOLOR=pink>")
OpenWindow.document.write("<h1>Hello!</h1>")
OpenWindow.document.write("This text will appear in the window!")
OpenWindow.document.write("</BODY>")
OpenWindow.document.write("</HTML>")

OpenWindow.document.close()
self.name="main"
     }
</SCRIPT>


     What we have above a very basic script written by Andree Growney and myself. This JavaScript creates a "function" that opens a new window. Look up at the top line of the script after the SCRIPT LANGUAGE="javascript" line.

     The function starts with the line: function openindex(). What that does in JavaScript speak is create a function called openindex. Then whatever is inside the { and the } following that statement is what the function is supposed to do. With me so far? In the case of this function, a new window will be opened up. The next lines of the script are quite similar to what we've seen so far:

OpenWindow=window.open("", "newwin", "height=250,
width=250,toolbar=no,scrollbars="+scroll+",menubar=no");

     The line calls for the creation of a new window. But, as you can see, there is no external page called for. There are just the quote marks. This forces the JavaScript to look at itself for the information.
     The name of the new window is "newwin", and the statements following define what the window will look like.
     Now we get to the meat of this little ditty. Each of the lines that start with:

OpenWindow.document.write()

     ...denote a new line of text that will be written onto the window that is being opened. Now look down the code, note the text inside the quotes of each new line is HTML code. Thus the text written to this window is going to read as HTML code. Now, I only have a few lines here, but you can add as many as you'd like. Just be sure to continue to preface each line with the OpenWindow.document.write command. Follow the format closely. One missing quote or parenthesis can kill the entire script.
     Be sure to end the script that same way I have above with the: OpenWindow.document.close()

Calling For The Function

     No, you're not done yet. Now that you have created and altered this JavaScript to your heart's content, it still will not work as is. You see, when you create a function such as this, something must be used to "trigger" the function to work. Usually, as is the case here, you use a command in the BODY portion of the main HTML document. So here's the deal...

  • Configure the above script and place it in the <HEAD> section of your main HTML Document.
  • To trigger the function, place this: onLoad="openindex()" in the BODY command of the main HTML document. That tells the browser to initiate the function "openindex" when the page is loaded.


Closing The Window

     This seems like the next logical step in the process. You made it open. Now let's make it close. There are three way of doing it:
  • Every window carries what I call the "goodbye box" on the upper right hand corner. It's the gray square that has a little "X" on it. Click that and away it goes.
  • You could offer a button on the page that closes it when the user clicks the button.
  • And you could set it up so that the window closes itself.

The Code To Close The Window

     I'm starting here because I don't think I need to go over the little "goodbye box". So...here's the code for the button:

<FORM>
<INPUT TYPE='BUTTON' VALUE='Close Window' onClick='window.close()'>
</FORM>

     Place that pup on the page that will go into the new window or place it in the code in the JavaScript format above and you get a little button that closes the window when clicked.

The Window Closes Itself

     OK - now we're getting into bigger and better JavaScripts. This little performance is too much to get into here, so I will send you to the small tutorial I posted on the Java Goodies site.

     The page is called Hello Goodbye. It's a great script written by William Flanery. Once you go, you'll see the effect, then have ability to grab the full working script.


Other Fun With New Windows

     What you can do with these little windows is really only limited to what you can think up. Here are a few other uses that you can take from my site.


A New Window Midi Trick

     I am asked at least once a day how to keep a midi playing in the background while a person surfs your site. It's a little rough because the page that has the midi embedded will stop playing the file once you leave it. So here's a suggestion.
     Place the embed commands on the page that will fill the new window. That way people can surf all over the place on the main browser window and the midi will play away. The only real downfall is that if the person closes the little window - the midi party stops cold.
     Eh. I said it was a suggestion, not the end all.


And That Does It...

     Exciting, huh? Now you can make little windows pop up all over town. Again I offer my suggestion that one new window might be a welcome help to the users of your pages. However a new window every other page would bring you to level of telemarketers in some people's minds. Use this only when they are really needed.

Enjoy!

[The Basic JavaScript Format]
[Configuring the Window]
[Page Inside A Page]
[Closing the Window]
[Other Fun With New Windows]
[A New Window Midi Trick]

Tools:
Add htmlgoodies.com to your favorites
Add htmlgoodies.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

IT Management Networking & Communications Web Development Hardware & Systems Software Development Earthwebnews.com



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES