html tutorials web design help css tutorials free web graphics diy search engine optimization

Create an Alert Box

Here's a very simple JavaScript snippet that will cause an alert box to pop up when your page loads, but then, you already saw it didn't you. Man, you're hard to surprise.

You can put most any text message you want in the alert box. It could be a welcome message, call attention to a new item or new page, or just tell someone their Aunt Fred wears army boots ...and she does, too. I've seen her in them.

The Dirt on the Alert

Just add this little JavaScript snippet to the BODY tag of your web page.
onLoad="alert('Put Your Way Cool Message Here')"
Where it says Put Your Way Cool Message Here is what will show up in the Alert Box. Two things to note:
  • The single quote marks ( ' ) before and after the message must remain in the code; and...
  • Because the single quote mark means something else in Javascript you can't use words like can't and don't in the message or they will cause an error. There is a way around that. By placing a backslash in front of the single quote it tells the browser to print that character rather than interpret it. So using a word like can't in the Alert Box would be written like this:
onLoad="alert('Never say you can\'t do it.')"

Alert When Leaving

To have an alert box pop-up when someone leaves the page instead of when they enter the page, change onLoad to onUnLoad.

That's all there is to it. Now go alert the world, you alarmist. Well . . . alertist didn't sound right.

Back | Web Design Tutorials Index

Ezine for Webmasters

Bookmark and Share

Almost a Newsletter

Changing list hosts. Will post a new subscribe form shortly.

Did you know...

The member's site has about 100 standards compliant HTML and CSS tutorials, 31 handy reference charts, reprintable content, web graphics, exclusive fonts, free software, free ebooks and more? All this for less than 9 cents a day! [ Details ]
See my fancy bottom! :)