Like HowStuffWorks on Facebook!

Why is it that some Web pages seem to trap me so I can't get out?

        Tech | Internet Basics

­ Many sites of ill repute use a technique that causes the following distressing situation: When you try to leave the site, either by using the Back button or by closing the browser window, the site reappears in a new window. In the worst cases, the site pops up in three or four new windows when you try to leave it, and basically the only way out is to shut down your computer. The way most people get trapped into these sites is through listings in search engines that look legitimate until you click on them.

The way these sites work is as follows. If you have read the HowStuffWorks article How Web Pages Work, you know that the HTML code for the simplest Web pages looks like this:

    Content of the page goes here...!

The tag marks the beginning of the body of the page. You are allowed to modify the body tag. For example, if you change it so it looks like , then the background color of the page will be yellow.

All major browsers today support a capability called JavaScript, and JavaScript allows you to embed code in a page. Using JavaScript, one of the modifiers you can add to the body tag is onUnload. For example, you can say:

This says, "When the browser attempts to unload the page (either by using the Back button or by closing the window), call the JavaScript function named replay." At the top of the page, the replay function is a small JavaScript function that looks like this:


When the replay function gets called, you can see that all it does is open a new window that contains whatever page is requested (here, it's There's nothing to stop the creator of the Web page from opening up multiple windows -- simply adding four more open lines to the replay function will cause five new windows to open. If they all do the same thing, your screen quickly fills with dozens of windows. That's when you have to shut down.

The use of the flag variable is interesting. That's there because if the page contains a link, it causes onUnload to get triggered when the link is clicked. So the link will contain the following code:

This turns off the replay function if the user clicks the link on the page.

There are all sorts of other less annoying things you can do with JavaScript. This page has lots of links.

These links will help you learn more:

More to Explore