Electric Type

Multimedia

About Us

News

Help

Controlling Who Sees What on Your Site

Page 3 — Password Protect with JavaScript

Recently, I found myself in an unlikely love quadrangle involving an Irish lad, an ex-boyfriend, and an adorable physicist. A minor fiasco involving the contents of my FAQ and the aforementioned gents proved that content not only isn't dead, but alive, well, and messing up my life. But it also caused me to rethink my disclosure policies. How do I feed my fans and stalkers the dynamic content they've come to depend on without losing my favorite pair of brown eyeballs? À la The Graduate, it comes down to one word: password.

Now if you want to set up a small, offshore money-laundering scheme or start your own country just to get a top-level domain with your initials, then you are probably going to need to implement some real security measures. But for the rest of us who just want to have nice, normal, fulfilling love lives, a simple password scheme can really deliver, if you know what I mean.

I realized that there are three kinds of Web surfers in this world: men I'd like to get to know better, the rest of the world, and my mom. Instead of jeopardizing my love life by baring my soul (and coffee intake) in a single daily FAQ, I needed to have three separate FAQs with appropriately varying content all hiding behind a single password-querying link.

If you've never worked with JavaScript before, I strongly recommend that you check out Thau's basic JavaScript tutorial. That's what I did. And here's how I went about password-protecting my love life.

First I rewrote the HREF link for my FAQ so that it calls a JavaScript function to ask users for a password and direct them to different FAQs based on their responses.

So instead of plain vanilla HTML,

<a href="./faq.html">
Read the Amazing Heidi FAQ
</a>
I associated a JavaScript function with the link:
<a href="./faq.html" onClick="whereTo();return false">
Read the Amazing Heidi FAQ
</a>
By leaving in the original FAQ URL, I made sure users without JavaScript will have an active link. Javascript-capable browsers will evaluate the onClick first, and as long as you include the return false at the end, you'll be golden. So all the Luddites will go straight to the default FAQ without having to sharpen their pencils, while JavaScript users will be sent wherever the whereTo() function knows to send them. I'd better show you how I wrote that script, too.

If you don't already know that the following JavaScript is customarily placed between the </title> and the </head> tags, you really should ask yourself, Why am I resisting the urge to check out Thau's tutorial?

<script language="JavaScript">
<!-- hide script
function whereTo()
{
var magic_word = prompt("What is the magic word?","Pretty Please?");
if(magic_word == "password")
{window.location = "./boy_faq.html";}
else if (magic_word == "mom")
{window.location = "./mom_faq.html";}
else
{window.location = "./faq.html";}
}
//end hiding script -->
</script>
The above function will prompt the user for a password. The password is then compared to the predefined options contained in your standard if-then-else script. When the user enters the password "mom," that user is presumably my mom and will therefore be sent off to the mom_faq.html document. Similarly, if the user types in the difficult-for-even-a-boy-to-forget password "password," he or she will be carefully directed to a tame version of my FAQ (boy_faq.html), where I would never ever reveal any potentially threatening emotions or needs. Enter any other password whatsoever, and the script assumes there are no special handling needs and takes you to the default all-access FAQ.

The above password scheme can be used to direct users to any number of fixed pages or folders. In some sense, this is not very different from simply handing out different URLs. However, one advantage of the password setup above is that you could have many password-isolated areas accessible from a single homepage, which makes for easier bookmarking and increased user friendliness. If you do plan to lock up sections of your Web site behind passwords, remember that you should have some kind of default page with a mailto link to accommodate random passers-by.

Again, what's to stop these people from viewing the source code and filching the passwords to see where they lead? The same thing that keeps people from opening medicine cabinets: nothing but a sense of decorum. Like I said, these methods are not meant to be used for securing your site absolutely; they are simply tools for tighter organization, content control, and of course, a better dating life.

next page»


Tutorials  

User Blogs  

Teaching Tools  

Authoring  

Design  

Programming help  

Advanced Flash  

Javascript  

Glossary  

PHP Coding  

User Blogs

Screen Shots

Latest Updates

Contact Us

Valid HTML 4.01!
Valid CSS!

Breadcrumb

© ElectricType
Maintained by My-Hosts.com
Site map | Copyright | Disclaimer
Privacy policy | Acceptable Use Policy
Legal information.