GSAnet Banner Swap





to tricks page

What is "correct" scripting?

Correct scripting is based on the idea of backward compatibility. This trick builds on the "how to check browser version" trick which you should read before reading this one, if you don't know how to check browser versions.

So what is correct scripting? Basically, you have several implementations of JavaScript and your task is to make sure that your page does not crash under any. JavaScript is a very naughty language in that it always starts to scream when it encounters an unknown operator or a command. So, for example, you want to use a command NewJavaProc(), that is only implemented in Netscape Navigator 4. The following code shows you how to make sure the command is not executed by any other JavaScript implementations.

<SCRIPT language="JavaScript">

function CorrectJavaProc(vars) {
if (version == "NN4") {
NewJavaProc(vars)}}

} //end of function

</SCRIPT>

... and then later on when the function is called

<A HREF="my.html" onMouseOver="CorrectJavaProc(12)"> Link to west </A>

Explanations:

Well, as usual, you start with SCRIPT tag. Now, you create a new procedure, that will act as an envelope procedure for the new JavaScript procedure, NewJavaProc(). This envelope procedure will only run NewJavaProc() if your browser is Netscape Navigator 4 (browser versions are explained in the "how to check browser version?" trick). Later on you call the envelope procedure CorrectJavaProc() in your HTML.

Now, why do you need to create envelope procedures? Envelope procedures are only used with events, such as onMouseOver or onMouseOut. These events are like standard HTML attributes but expect a JavaScript procedure as a value, e.g. onMouseOver="status(10)".

And if an older browser encounters a new (unknown) procedure as a value, it will generate an error and user will be presented with an annoying JavaScript debug window. To get away with running a new procedure as a value, you create your own procedure (CorrectJavaProc(), and use it to call the new procedure (NewJavaProc). The procedure (CorrectJavaProc()) is then called by an event and an older browser is prevented from executing the new procedure.

If you don't know what events are, you probably do not need to use this trick, because if you want to prevent an old browser from executing a SCRIPT, you simply can use a language attribute of a SCRIPT tag. Events are different in that they are not a part of SCRIPT tag, they are a part of HTML.

Finally, if you are still shaky about it, wait for a trick that explains how you can use JavaScript events, that is being written as you read this.


©1997 repfect Drug design studio. All Rights Reserved.