Free Web tutorials covering HTML, CSS, JavaScript, and DHTML from beginner to advanced. Free downloads and developer resources. Personalized help via email, form, and chat.

free, web, tutorials, HTML, html, CSS, css, stylesheet, cascading stylesheet, Javascript, javascript, JavaScript, DHTML, dhtml, beginner, advanced, web development, web page, web site, free web tutorial, free HTML tutorial, free CSS tutorial, free css tutorial, free cascading stylesheet tutorial, free stylesheet tutorial, free javascript tutorial, free DHTML tutorial, free HTML class, free CSS class, free stylesheet class, free cascading stylesheet class, free javascript class, free DHTML class">

Free Web tutorials covering HTML, CSS, JavaScript, and DHTML from beginner to advanced. Free downloads and developer resources. Personalized help via email, form, and chat. free, web, tutorials, HTML, html, CSS, css, stylesheet, cascading stylesheet, Javascript, javascript, JavaScript, DHTML, dhtml, beginner, advanced, web development, web page, web site, free web tutorial, free HTML tutorial, free CSS tutorial, free css tutorial, free cascading stylesheet tutorial, free stylesheet tutorial, free javascript tutorial, free DHTML tutorial, free HTML class, free CSS class, free stylesheet class, free cascading stylesheet class, free javascript class, free DHTML class

<Code_Punk>'s

Beginning JavaScript Lesson 19:
Basic Pop Up For Internet Explorer

Code Tutorials



Site Development



Downloads



Help!!



Home

Making The Box & Link

You should already be able to make a <div> box. If not, review Advanced CSS until you can make, style, and position one.

If you are already familiar with <div> boxes, you'll be surprised at how easy pop up menus and boxes are to make. Here's a simple example. All you do is make a box, set its visibility to "hidden", and use JavaScript to switch the box between "visible" and "hidden" visibility property values.

Lets begin by making a simple page with the following in the <body>:

<a href="javascript://" onMouseOver="show_it()" onMouseOut="hide_it()">
Hover Here For Pop Up</a><br><br>

<div id="box1">
<h2>Hi There. I'm a pop up box.</h2>
</div>

We'll be coding the functions "show_it()" and "hide_it()" to manipulate our box in a minute. The <div> ID'd as "box1" will be the pop up box. For most pop up applications, you'll be giving <div> boxes individual, unique IDs.

Styling The Box

You can style the box and contents any way you like. Two important things to include in the box styling are its position and visibility:

#box1 {
position: absolute;
top: 50px;
left: 100px;
border: solid 3px #000000;
background-color: #ffff00;
visibility: hidden;

You will be using "position: absolute" for most of the pop up boxes you code. This means you'll need to position the box with "top:" and "left:".

A good method for making these pop up boxes is to make and style the box and its contents first. Next, position the box. Finally, set the visibility to "hidden" to make the box invisible. Now lets pop it up by making it visible again.

You may find it easier to begin styling the box by first styling its look: borders, background. Then position it, and finally set "visibility: hidden" to hide the box.

document.all

You can access CSS styling and positioning properties and values with JavaScript. For Internet Explorer these properties can be assigned using the "document.all" object. The "document.all" object contains all styled objects, including <div>s coded with an ID.

What we want to do is to access a <div>s "visibility" property. Below is the specific reference we'll use in our current example:

document.all.box1.style.visibility = "visible";

The above line of JavaScript would make our box appear, or pop up. In other words, it dynamically changes the "visibility:" property to a value of "visible".

Here's the generic form for accessing and changing CSS properties in Internet Explorer:

document.all.element_id.style.property = new property value

You can use JavaScript to change styling by using an element's ID among other identifiers, but ID is the most used in making pop ups because individual boxes are referenced.

As we'll see in future lessons, the syntax for the property name changes some between CSS and JavaScript. In the case of "visibility" the property is spelled "visibility" for both CSS and JavaScript.

This isn't always the case. For some CSS properties, like "background-image", this is not true. The JavaScript reference for the "background-image" property is "backgroundImage". There is no hyphen and a capital "I". Don't worry about these for now, but keep in mind that there are differences.

The values assigned to CSS properties with JavaScript are generally strings that must be placed inside quotes.

Popping Up

With the above overview out of the way, we're ready to make our box pop up. Remember that the box should already be invisible because we've set "visibility: hidden" in our CSS styling.

In the <head> of the page code the following to create the needed "show_it()" function:

<script language="JavaScript">

function show_it(){
document.all.box1.style.visibility = "visible";
}

</script>

The above is all it takes to make our box become visible, or pop up, when the text link is hovered over. Remember that we're coding the effect only for Internet Explorer in this lesson. We'll tweak it for all browsers in the next lesson.

Disappearing

Now lets make the box disappear when the cursor is moved away from the link. We've already coded an onMouseOut link in our <a> tag, so all we need to do is add the "hide_it()" function to our <script>:

function hide_it(){
document.all.box1.style.visibility = "hidden";
}

This is just like making the box pop up except we change the value to "hidden". The rest of the object reference is the same. It should be since we are referencing the same object and property. We're just changing th value.

Summary & Exercises

Follow the four steps below when coding pop up boxes/menus.

1) Decide what events you will use to show and hide the box. We used onMouseOver and onMouseOut in our example. We'll discuss more about this later, but it's an important first step so you'll know what tags to code the events in. Almost all pop up boxes are triggered by an event coded in an <a> tag.

2) Code the box. Make sure to include an ID in the opening <div> tag. Since the box will be styled with an absolute position to place it on the page, it doesn't matter where you actually place the <div> in the code. Most folks put them at the bottom of their <body>s so the <div> code doesn't clutter the other HTML coding.

3) Style the box. Begin by coding the borders, background, and dimensions. Get the box looking good and sized first. Now, position the box with "top:" and "left:". Tweak as needed. Finally, add "visibility: hidden" to make the box invisible.

4) Make the JavaScript functions to show and hide the pop up.

Practice making some single pop up boxes and try your hand at coding two boxes on the same page. Try to find ways to let one, single function control both the hiding and showing of multiple boxes. Don't worry if you get stumped, we'll be covering this later.



To Next Beginning JavaScript Lesson

Back To Beginning JavaScript Index