Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Toggling Images and Text in Internet Explorer

First Things First: This is a tutorial dealing with DHTML. You need to be running an Internet Explorer browser, 4.0 level or better, to see the effect.

You can find this tutorial, and all of its examples, online at http://www.htmlgoodies.com/beyond/toggle.html.

You can download just the examples at http://www.htmlgoodies.com/wpg/.

This tutorial is basically a DHTML session. What I'm going to show you here is how to make a division appear and disappear in MSIE. I also have a sister tutorial to this one that teaches you how to make a layer appear and disappear in Netscape Navigator. It's covered in the next section, "Toggling Netscape's Layers." The effect is the same, but all the commands that do the trick are different, so multiple companies have a hand in the process. Basically, this means that I'm about to get a bunch of letters that tell me that this effect is actually called "visibility", "layering," or "Steve."

The truth is because the MSIE and Navigator browsers are moving in such different directions, it's hard to create one definitive statement that covers the effect. In one of my computer books, the author refers to making a layer appear and disappear as "toggling." I thought was as good a term as any.

But no matter what you name it—the effect will still be as sweet (to paraphrase Bill Shakespeare—I'm a cultured man you know).

Toggling with MSIE

In Microsoft Internet Explorer, you get the effect through DHTML commands. Now remember that these commands are only supported in IE 4.0 and above and are not supported in Navigator (as of 10/20/01). So when you set up this effect, make sure that the users are running IE 4 or better. You can do that through setting up a browser detect script. If you don't, errors fly all over the place. This is a good one to make sure that your people are prepared for.

Look carefully at Figures 3.193.21. The effects are demonstrated here.

Figure 3.19Figure 3.19 Notice that my pointer is off the text "Hey Man!".


Figure 3.20Figure 3.20 Now the pointer is on top of the text, and the box popped up.


Figure 3.21Figure 3.21 I've gotten the box to pop up now by using the button. Notice that the other button will take it away.


It's not that difficult of an effect either. Basically what's happening is that I have positioned a division on the page. In that division, I put a table cell with the words "How About This?" inside, but just about anything can be put in the division.

Here's the entire script (I break it apart just after the listing):

<script language="JavaScript">
function ShowIt()
{
document.body.insertAdjacentHTML('BeforeEnd','
<DIV STYLE="position:absolute; TOP:35px; LEFT:410px"
 ID="TheTip"><TABLE BORDER="1" 
CELLPADDING="3"> <TD BGCOLOR="ff00ff">How About This?</TD></TABLE></DIV>');
}
function LoseIt()
{
TheTip.innerHTML = " ";
TheTip.outerHTML = " ";
}
</script>
<center><a href="http://www.htmlgoodies.com" 
onMouseOver="ShowIt()" onMouseOut="LoseIt()">Hey Man!</A>
</center>
<P>&nbsp;<P>
<FORM>
<INPUT TYPE="button" Value="let me See It" 
onClick="ShowIt()">
<INPUT TYPE="button" Value="OK, Take It Away" 
onClick="LoseIt()">
</FORM>

Make the Division Appear

After the division was created, I encased it in a JavaScript function so that I could call on it whenever it is clicked or moused over.

Next, I set up another JavaScript function so that when the mouse moved off the link, the division disappeared again.

After I have a function set up that makes the division appear and disappear, the process is simple. Call the correct function, and the effect comes to life. Well, it's relatively simple anyway.

Make It Appear

The harder of the two functions is the one that makes the division appear, so we'll start with that one. It goes up in between the <HEAD> flags and it looks like this:

<SCRIPT LANGUAGE="javascript">
function ShowIt()
{
document.body.insertAdjacentHTML('BeforeEnd', '<
DIV STYLE="position:absolute; TOP:35px; LEFT:410px" 
ID="TheTip"> <TABLE BORDER="1" 
CELLPADDING="3"> <TD BGCOLOR="ff00ff">How About This?
</TD></TABLE></DIV>');
}
</SCRIPT>

That one line is pretty long, huh? Yeah. It can be broken down, if you really want, into multiple document.write statements, but why? It's just more typing for the same effect.

So, what does it do? Nothing. It won't do anything until it's called on by its function name later in the page. Let's tear it apart even more.

This is a JavaScript, so we have to start with the familiar "SCRIPT LANGUAGE=" flag.

The function is named ShowIt(). Note that fancy brackets always surround the JavaScript commands making up the event that the function performs.

Now here's the magic—we begin with a hierarchy statement that uses commands which are proprietary to MSIE. That's a nice way of saying that only Explorer understands them. It's DHTML.

document.body.insertAdjacentHTML represents to the IE browser that whatever follows is to go on the document, in the body, and what follows in parentheses is to be inserted as HTML.

In case you're wondering, and I know you are, there's also the command, insertAdjacentText. It works the same way except that it handles what appears in the following parentheses as text alone and does not compile it into HTML.

Inside the parentheses, the first command deals with where this little division should display in terms of the command that is calling for it. It doesn't come into play much in this scenario because we are calling on this division from a function and not from inside of an HTML command. But you still need to put something in there to denote where the inserted HTML will appear, or the format throws an error.

'BeforeEnd' means that the division should appear at the end of the element before the end tag. There are actually three others you can play with if you take this format and embed it in to an HTML flag:

  • BeforeBegin—The item is inserted in front of the flag.

  • AfterBegin—The item is inserted after the flag, but before the text.

  • AfterEnd—The items is inserted after the end tag.

Now we get to the element that will be inserted. It's a division that has been positioned and given the NAME "TheTip" so that we can call on it later. It looks like this:

<DIV STYLE="position:absolute; TOP:35px; LEFT:410px" ID="TheTip">

In terms of the effect, the positioning is very important. If you decide to have multiple divisions popping up over a series of links, you need to have each one positioned so that they pop up at the right place.

Or, as I've seen it done, have them all appear in the exact same place, which is a great effect. One just lays right over the other. It's like a little billboard popping up.

You know what I've found with positioning? It's best to be most concerned with the pixels from the top and go real easy on the pixels from the left. Also, go easy on the concept of absolute positioning. There are too many screen resolutions and sizes out there to be overly concerned. Use the command positioning:absolute, but keep in mind that you're only going to get "pretty close" positioning. It'll keep your blood pressure down.

What follows in the division is a basic one-celled table with a purple background. It might look a little strange because it is all on one line, but that's all it is.

The </DIV> flag kills the line of text.

The second curly bracket and the </SCRIPT> wrap up the entire format.

Now take that function, stick it in between <SCRIPT LANGUAGE="javascript"> and </SCRIPT> commands, and put that between the <HEAD> flags. So now, you understand and posses a function that will make the division appear. But can you make it disappear again? Read on to find out how.

Make the Division Disappear

What we need to do is set up another function.

function LoseIt()
{
TheTip.innerHTML = " ";
TheTip.outerHTML = " ";
}

This one's pretty easy to figure out even if DHTML is brand new to you. The function, named LoseIt(), simply sets two sections of the division to represent nothing. In other words, it disappears.

Remember, the name of the division is "TheTip". Go ahead and look at the "appear" function again if you missed that point. It's important. In this function, we set two parameters, innerHTML and outerHTML, to nothing. Note that the quote marks contain only an empty space. The end. There's no more visible division. That's very clever.

Now, take that code, stick it between <SCRIPT LANGUAGE="javascript"> and </SCRIPT> commands, and put that in between the <HEAD> flags.

OK, now we're set. We can call for the division in the first function any darn time we feel like it.

Call for the Division

Now that we have the two functions just waiting to be used, we can call for them as we would any other function. In the two examples shown in this tutorial, I set up a rollover on a hypertext link and also made the division appear through the use of a form button. Here's the code for each.

The Hypertext Link:

<A HREF="http://www.htmlgoodies.com" 
onMouseOver="ShowIt()" onMouseOut="LoseIt()">Hey Man!</A> 

The Form Buttons

<FORM>
<INPUT TYPE="button" Value="let me see it" 
onClick="ShowIt()">
<INPUT TYPE="button" Value="OK, Take it Away" 
onClick="LoseIt()">
</FORM>

There's no real science to it. I've called for the functions through basic onMouseOver, onMouseOut, and onClick Event Handlers depending on how the user would get the effect.

More Divisions

This is a great effect if you have a series of links down one side of the page. The effect of multiple divisions appearing one after the other looks high-tech and appears interactive.

The only downfall, if you want to call it that, is that each of these divisions is an element in its own right. They each have a NAME attribute assigned. Thus, you need to create a totally new function to make the division appear and disappear.

For example, let's say that you already have the division described in this tutorial installed on a page. You want a second one. Here's what you need to do:

Create a whole new function that makes the division appear. The easiest method would be to copy and paste the current "appear" function and change its name. The current appear function is named ShowIt(). You could simply change the name to ShowIt2().

You need to go in to the division itself and change out:

  • The TOP and LEFT positioning pixels

  • The NAME of the division

  • What is contained in the division

Finally, copy and paste the function that makes the first one disappear. Again, you need to make a few changes:

  • You need to change the name of the function. The current function is named LoseIt(). You could simply change the new function name to LoseIt2().

  • You need to change the NAME element of the innerHTML and outerHTML statements. Remember that they are currently attached to the first division named "TheTip".

  • You need to change "TheTip" to whatever you named this new division.

Now you're good to go with a second division. Yes, it's a little work, but the results are great.

A Final Note

While working on this tutorial, I played with multiple and single divisions. I can honestly say that what makes these things really shine is the positioning element. Where they pop up is really the point of all this, more so than the fact that they pop up at all. I found that you couldn't be overly precise. Get close. I loved the look of an element on the left side of the page popping a window on the right.

Got it? Great! Now learn how to toggle with Netscape's Layers.

  • + Share This
  • 🔖 Save To Your Account