HTMLGoodies
The ultimate html resource
Earthweb.com


About the Double-Underlined Links


Become a Partner




Search Clipart.com:



internet.commerce















HTML Goodies : Primers : Javascript Primers: JavaScript Primers #12

HTML GOODIES TO GO NEWSLETTER


Other Related Newsletters

JavaScript Primers #12


By Joe Burns

Use these to jump around or read it all
The Concept
The Script
The Script's Effect
Deconstructing the Script
What You've Learned
Your Assignment


The Concept

In Primer 10 we opened a new window using the "window.open" command. That window was then filled with another HTML document we named in the instance.

Here, we're going to create a new window function where the new window and all of its contents will be carried along in the same HTML document. It is literally the equivalent of two pages in one.


The Script


<SCRIPT type="text/javascript">

function openindex()
    {  

var OpenWindow=window.open("", "newwin", 
"height=300,width=300");

OpenWindow.document.write("<HTML>")

OpenWindow.document.write("<TITLE>New Window</TITLE>")

OpenWindow.document.write("<BODY BGCOLOR='00ffff'>")

OpenWindow.document.write("<CENTER>")

OpenWindow.document.write("<font size=+1>
New Window</font><P>")

OpenWindow.document.write("<a href=
'http://www.htmlgoodies.com' target='main'>
This will open<BR> in the main window</a><p>")

OpenWindow.document.write("<P><HR WIDTH='60%'><P>")

OpenWindow.document.write("<a href='' 
onClick='self.close()'>
This closes the window</a><p>")

OpenWindow.document.write("</CENTER>")
      
   OpenWindow.document.write("</HTML>")

    }

</SCRIPT>

...and in the BODY command:

onLoad="openindex()"



The Script's Effect

The effect is exactly the same as in Primer 10: The same sized window opened and it contained the same two links. The difference is that it was all done with one page. To see the script on this page in action, click here.



Deconstructing the Script

The main script, the part that contains the function, is placed in between the <HEAD> and </HEAD> tags, as are most functions.

The function is named "openindex()" in the normal fashion. Then the fancy parentheses go in to surround the following commands.

Now we get to the meat of it. The variable "OpenWindow" is created that will act as the window.open("instance") command. It looks like this:

var OpenWindow=window.open("", "newwin", "height=300,width=300");

The format is familiar. The only real difference is that there is no URL denoted. See the empty double quotes? That tells the browser to look to the script to find the new window information. It's very similar to not placing a URL in the command that closes the window. It wouldn't close if it had something to load. Same here. It wouldn't look to the script if it had something to load.

Now we start to build the HTML page that will go inside the new window. Here's the first line of text:

OpenWindow.document.write("<HTML>")

This format should also look somewhat familiar. The command is saying that on the variable "OpenWindow" (the new window) this link of text should be written to the document.

Look back up at the full script. That format is followed again and again and again writing lines of text. There's no reason why there cannot be hundreds of lines of text creating a fully functioning HTML document. Mine is small because it's a primer example.

Remember: When you are writing HTML inside a "document.write" command, you cannot use double quotes to surround subcommands. Use single. If you don't, error.

Finally, the function is called for in the BODY command through an "onLoad" Event Handler.


What You Have Learned


Your Assignment

For today's assignment, you'll create a window that opens using a function. Please give the document that appears in the window a green background. In addition, make the TITLE command read "Hello user name - Here is your window! You can gather the user's name through a prompt. Of course, make a link that closes the window.

Here's a possible answer
(this will open a new window)


The Concept
The Script
The Script's Effect
Deconstructing the Script
What You've Learned
Your Assignment

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
IBM Whitepaper: Service Component Architecture Enabling XML Web Services for Java Programmers
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Intel Article: Using Power & Display Context in the Intel Mobile Platform SDK
Internet.com eBook: Real Life Rails
IBM SCA Center Article: Simplifying Composite Applications with Service Component Architecture
Intel PDF: Quad-Core Impacts More Than the Data Center
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Intel PDF: Analysis of Early Testing of Intel vPro in Large IT Departments
Internet.com eBook: Best Practices for Developing a Web Site
Intel PDF: IT Agility through Automated, Policy-based Virtual Infrastructure
IBM CIO Whitepaper: The New Information Agenda. Do You Have One?
Microsoft Article: BitLocker Brings Encryption to Windows Server 2008
IBM Whitepaper: Service Component Architecture & Java EE Integration
Microsoft Article: RODCs Transform Branch Office Security
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
Avaya Article: Advancing the State of the Art in Customer Service
IBM Whitepaper: How are other CIOs driving growth?
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Avaya Article: Avaya AE Services Provide Rapid Telephony Integration with Facebook
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Download: IBM WebSphere Application Server V7.0 Feature Pack for Service Component Architecture
Actuate Download: Free Visual Report Development Tool
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
IBM SCA Download: Start Building SCA Applications Today
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES