Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


Ask Web site visitors if they're over 18 on page load?

I have a Web site where I want to check and ensure visitors are over 18 before they proceed and am not sure of the best way to accomplish this. I'm thinking a little pop-up JavaScript window would be cool, but have no idea how to proceed. Help!


Dave's Answer:

Without going into any sort of discussion on what kind of site we're talking about ('nuf said on that topic, I suspect) there are a couple of ways you can accomplish this. The easiest is to have your home page simply indicate the kind of content they're about to see and offer two options for proceeding: if you're over 18, click to get to our "real" home page, if you're not, click to go to Google.com or some other innocuous Web site.

That's a simple and straightforward solution that has the benefit of letting you mask your content from younger eyes too, so it's what I would suggest, but if you really do want to create a JavaScript pop-up window, you'd want to tie it to the onload event, which means that the page will fully render, images and all, then the window will pop up. Think about that.

Still want to proceed? Okay, here's some JavaScript code:

<html>
<head>
<title>test page</title>
<script type="text/javascript" charset="utf-8">
function doOnLoad()
{
if (confirm("Do you want to see this page??")) {
self.location="http://www.google.com/"
};
}
</script>
</head>
<body onload="doOnLoad()">
<h1>This is a test page</h1>
If this is working properly, the page will load, then a window will pop up
asking "do you want to see this page?" If you answer affirmatively, the
window goes away. If you say no, then you're bounced to Google.
</body>
</html>

In a nutshell, what this does is invoke "doOnLoad" when the page has fully loaded (which is why it has to appear in the "body" HTML tag), which uses the built-in confirm() function to put up a dialog box like this:

web page open popup

It's not a great solution as you can see because "Cancel" means you're going to stay on the page and "Okay" means you're leaving, so it's going to be tricky to word a question where that then makes sense. Perhaps "If you're under 18, please click on "Okay", otherwise we'll assume that you are old enough to see the content of this page." but even that's pretty awkward.

There are more sophisticated solutions you could utilize, including a DHTML layer pop-up that grayed out the underlying page and had logical yes/no button labels, but that's a lot more work and it might well not be worth the hassle. Again, I suggest that having a different home page that offers two options for proceeding is likely your best solution in this situation.

Thanks to Darrell Brogdon, Robert Bell and Luke Kingland for their suggestions on the above JavaScript code.

More Useful HTML, JavaScript and Web Site Programming Articles:
✔   How to Create Predefined Google Image Search Links?
Thanks for the Amazon URL [see Creating Amazon Search Links]. That worked beautifully. In fact, I sent you $5.00 for coffee in thanks....
✔   Can I embed a Facebook search box on my blog site?
I've seen your articles about how to add a Twitter or Google search box on a Web page, but I have a tougher...
✔   Can I use CSS for drop shadows on my blog?
I want to give my site a bit of a facelift and add some neat graphical elements. One of which is drop shadows....
✔   How can I embed interactive photo panoramas on my site/blog?
I read through your blog entry about how to take panoramic photos with iOS 6 and an iPhone 5 and got enthused. I've...
✔   How can I create a Twitter search URL shortcut?
I'd like to add a few Twitter search links to my Web site. Is that possible, or does Twitter prohibit this sort of...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  





Categorized: HTML, JavaScript and Web Site Programming   (Article 9598, Written by )
Tagged: content filtering, content management, javascript, web page design
Previous: How can I limit Facebook status updates to certain friends?
Next: How do I copy my Firefox bookmarks into Safari?




Reader Comments To Date: 3

Ken B said, on September 24, 2010 8:19 AM:

What's wrong with redirecting to a different site it they press Cancel?

if ( ! confirm("Do you want to see this page??")) {
self.location="http://www.google.com/"
};

I guess the downside is that the "ok" button is the default, making it too easy to click "by accident"?

I agree that it's better to not have the page appear at all, until they claim that they are over 18.

Of course, then you have the whole question of "what prevents a 17-year-old from clicking 'yes, I'm 18'?"

Luy said, on October 7, 2010 2:15 AM:

I just stumbled upon a pretty neat way of doing this using JQuery and cookies which seems easy enough:
http://www.techerator.com/2010/09/how-to-perform-age-verification-with-jquery-and-cookies-mmm-cookies/

The advantage here is that your users can come back to the home page without being bothered again, AND internal pages on your site can also be age restricted.

Olayinka temi said, on December 7, 2011 8:37 PM:

How do i sign up

Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!

I do have a comment, now that you mention it!











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.