--------------------------------------------------------------------------------
This intermediate page is what does all the work. As you can see, if the password is correct, it takes the user to the protected page. In the example, the protected page was "jex11.htm". You can replace that with the url of the page you wish to protect.
If the password is incorrect, the user gets sent back to the page that contains your link to the intermediate page. In my case, that is the very page you are looking at, "jpass2.htm".
Well, give it a try and see if it works better for you. Have fun!
So, why is it easy to hack the script? One way is for the viewer to disable javascript. Not only will they get to the page this way, they can also view the source code to see the password and use it later. Thus, if you are protecting something important, you should use something more secure.
A javascript confirmation box can be a handy way to give your visitors a choice of whether or not an action is performed. A confirmation box will pop up much like an alert box, but will allow the viewer to press an "OK" or "Cancel" button. Here is the basic command for creating a confirmation box:
confirm("Text or question you want to use");
The trouble is, if you use just that it isn't very useful. The confirmation box will return a value of true or false, so this is what we must use to make use of the confirmation box. An easy way to get the value for later use is to assign it to a variable, like this:
var where_to= confirm("Do you really want to go to this page??");
Now, you can use the where_to variable to send the user to one page or another, depending on the value the confirmation box returned. You can do this with an if/else block:
if (where_to== true)
{
window.location="http://yourplace.com/yourpage.htm";
}
else
{
window.location="http://www.barbie.com";
}
In this case, if the viewer hits the "OK" button, the browser will go to your special page. If the viewer hits the cancel button, the browser takes the viewer on a fun trip to the Barbie web site!
To make use of it though, you'll probably want to write a JavaScript function in your head section that you can call in the body of the page. You could place the items above into a function like this, to go inside your head tags:
Now, you need to find a way to call the function inside the body of your document so that you can offer the viewer access to the special page. You can use a button to call the function when it is clicked, so you would place something like this in the body section:
To go to my special page, click the button below:
This would work like the example button below, give it a try!
To go to my special page, click the button below:
As you can see, this may come in handy for something you want to code down the line. Of course, I can't confirm that...
Browser detection allows you to find out what browser your viewer is using, and then perform a script based on it-- or just to send a friendly message to those with your favorite browser.
There are two objects often used for this, the navigator.appName and navigator.appVersion objects. The first one returns the name of the browser, the second returns the version of the browser.
If the browser is Netscape, navigator.appName returns the string "Netscape". If it is Internet Explorer, it returns the string "Microsoft Internet Explorer". Using just this, you could make a script to alert people as to what browser they are using (just to bug them). Like this:
You can do the same thing with the navigator.appVersion, except you will most likely want to grab just the integer from the version information (2,3,4, etc.). To do this, we use the parseInt() function:
var browserVer=parseInt(navigator.appVersion);
Now, it returns only the integer value and not something like version 4.51. It just sends back 4 in that case. Thus, we could alert viewers as to whether their browser is new enough for us or not:
Of course, you can use both objects to be more exact. You could look for a certain set of browsers and only if they are above a certain version:
As you can see, that uses a lot of the logical operators from a previous section. It is basically saying that "if the browser name is Netscape and it is version 3 or greater, OR if the browser name is Microsoft Internet Explorer and the version is 4 or greater, then we will assign the variable version as 'n3'. Otherwise, it is going to be 'n2.' Then we move on to the rest."
One of the more common uses for browser detection is to redirect a viewer to a page made for his/her browser type or browser version. We will discuss that in the next section on redirection.
Note: Be careful using this if the page you use it on is listed in a search engine. Many of them find redirection to be a way of attempting to spam their index, and may remove the site from the listings. If you are unsure, don't try this-- or contact the search engine for their rules.
Redirection is often used to take viewers to a page depending on their browser's name or version. To redirect a viewer instantly, you just need to add a short command in your head section:
This would take the viewer right to the url you used as soon as they start loading the page. Sometimes this is done if a site has moved to another location. Another use for it is to detect a browser type and redirect your viewers to one page for Netscape, another of Internet Explorer, or a third for other browsers:
This uses the browser detection method from the previous section. Rather than popping up an alert box, we redirect the viewer to a page that best suits the browser being used.
If you want to have one page for version 4 browsers and another for others, we can take another script from the previous section and change it in the same way:
Not too bad, the only trouble is the need to create so many pages!
You can also use this to help people who come into your site, but come in on a page that should be within a frameset that you use for navigation. Using the top.frames.length object, you can find out if the page is in a frame or not. If the value is zero, the page is not in a frame. If it is greater than zero, the page is inside a frame. So, you could take the viewer back to your main frameset page using a script like this:
This will alert the viewer and take them to the main page after 10 seconds. You can change the number in the setTimeout function to adjust the time if you like.
You can also use it to break a viewer out of someone else's frames if they followed a link that didn't have the target set correctly. It uses the same idea in reverse:
As you can see, redirection can be a handy tool at times. Just use it with caution if you are worried about the search engines.
To open a new window, you will need to use yet another ready-made JavaScript function. Here is what it looks like:
window.open('url to open','window name','attribute1,attribute2')
This is the function that allows you to open a new browser window for the viewer to use. Note that all the names and attributes are separated with a comma rather than spaces. Here is what all the stuff inside is:
'url to open'
This is the web address of the page you wish to appear in the new window.
'window name'
You can name your window whatever you like, in case you need to make a reference to the window later.
'attribute1,attribute2'
As with alot of other things, you have a choice of attributes you can adjust.
Window Attributes
Below is a list of the attributes you can use:
width=300
Use this to define the width of the new window.
height=200
Use this to define the height of the new window.
resizable=yes or no
Use this to control whether or not you want the user to be able to resize the window.
scrollbars=yes or no
This lets you decide whether or not to have scrollbars on the window.
toolbar=yes or no
Whether or not the new window should have the browser navigation bar at the top (The back, foward, stop buttons..etc.).
location=yes or no
Whether or not you wish to show the location box with the current url (The place to type http://address).
directories=yes or no
Whether or not the window should show the extra buttons. (what's cool, personal buttons, etc...).
status=yes or no
Whether or not to show the window status bar at the bottom of the window.
menubar=yes or no
Whether or not to show the menus at the top of the window (File, Edit, etc...).
copyhistory=yes or no
Whether or not to copy the old browser window's history list to the new window.
All right, here's an example code for opening a new window:
Test it out below:
Yes, you got a 400 by 200 window with some writing in it!
Some Important Rules
Before we move on, we need to make note of some things so you won't go insane like I did trying to get this to work right!
When you get to the INPUT tag, keep everything in that tag on one single line in your text editor, including the javascript commands. (The text goes to the next line on this page so you can print it out easily).
Once you come to the onClick=" ", don't leave any spaces between anything. Just use the commas and the quote marks. Any white space will keep it from working correctly in Netscape.
Don't put quote marks around the yes, no, or numbers for the attributes. You only use single quotes around the entire set of attributes.
In some browsers, you may need to substitute the number 1 for yes, and the number zero for no in the attributes section. The yes or no should work fine, though.
A New Browser Window
Okay, enough rules. Let's look at the code that makes a completely new browser! Basically, you just use yes for all of the attributes. Here is the code:
Give it a try, this window has all the features!
Remember, keep everything on one line....one really, really long line! I just put the sample code on new lines so you wouldn't have to scroll forever to read everything........and your printer won't go crazy now either!
Closing a New Window
Hmm.....what's with the "Close Window" button you saw in the new window? How does one do do that? To use that trick, use the window.close() function in the HTML of the new window. Just put this code wherever you want the close button to show up in the new window:
Of course, the window can be closed with the "x" symbol on the top-right of the window as well.
Set the Window Position
There is another set of options you can use to set the position of the new window on the viewers, but it only works with NS4+ and IE4+:
screenX=number in pixels
Sets the position of the window in pixels from the left of the screen in Netscape 4+.
screenY=number in pixels
Sets the position of the window in pixels from the top of the screen in Netscape 4+.
left=number in pixels
Sets the position of the window in pixels from the left of the screen in IE 4+.
top=number in pixels
Sets the position of the window in pixels from the top of the screen in IE 4+.
Great, but how do you decide which commands to use if there are different ones for each browser? In this case, you can use both sets of commands- the browser will ignore the set it does not recognize. The example below will give you a new window 0 pixels from the left and 100 pixels from the top of your screen:
Now, that is a lot of work- but you can now customize a new window for your viewers!
I get this question so much, I figured I'd better get in gear and write another section to address using the link tag for javascripts (such as new windows), rather than using the old grey button. Well, there are a couple of ways to do this. I'll start with the easier to understand version first.
The first method is to access a javascript function within the HREF attribute of your link tag. So, if you want to link to another page, you normally write:
Click here
Well, you can access a javascript function you have written instead by writing the link this way:
Click Here
Yes, now you can open that new window without using the grey button. Here is a script to give you the new window:
First, I found that this works much better if you create your own function in the head section first. All this function needs to do is open your new window. So, in your head section, create a function like this:
The above script will open my "jex5.htm" page in a new window. As you know, replace this with url of the page you wish to open, and adjust the other attributes to your liking. If you need to know more about the window.open function, see the Opening a New Window tutorial and learn that first.....then come back and get going with the rest of this section.
Now go into your body section to wherever you want the link to appear, and write your link tag this way:
Click Here!
Now you will get a link like the one below. Give it a try and see the new window appear when you click the link!
Click Here!
For those of you who want to use an image for the link, just do the normal "image inside the link tag" trick, but with the link tag modified for javascript like above:

Now you can click on the image below for a new window!
The second way to do this is a little more difficult, but some people may be more comfortable with it. The trick is to go ahead and use the onClick=" " attribute in your link tag. The trick here is to keep the browser from following the actual link after running your script. Here is a sample of using the onClick attribute in the link tag:
Click Here!
I used the same script we had written in the head section for the first method, but I used it inside the onClick=" " command. Also notice the semicolon after the function call, and the "return false" at the end of the command. The return false part keeps the browser from going to "newpage.htm" after opening your new window. You could put any page here you want, and the link will no longer take you there (except in some older browsers). So you don't really have to put an actual url in the HREF attribute here unless you wish to offer an alternative for those with older browsers that don't recognize the onClick=" " command. As in the above example, you can also use an image inside your link tag to make a clickable image. Below is an example link where I used this second method:
Good work! You have finished the basic javascript tutorial section. If you went through the HTML tutorials here, you got a similar page when you finshed the HTML basics. Yes, that page also had a gold background. So, once you have readjusted your eyes, you can go back to the JS main page or move on to the intermediate/advanced JS tutorials. Now, doesn't that sound like fun??