Home > Articles

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.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020