Mozilla. This in general covers both Mozilla and Mozilla Firefox, and often also includes other Mozilla based browsers such as K-Melon, Galeon, Camino. <http://mozilla.org/products/>, Any platform.
Microsoft Internet Explorer in general. n stands for version number.
The most common is to insert alerts into the code, where you alert the values and types of variables, function arguments, and object properties. If you are doing code forking to support different ways of doing things, you may use confirms to force a certain path to be taken. If you want to be able to cut and paste the results, you may want to use prompts.
In an effort to get better error reporting you may use window.onerror or the try..catch statement. These may also be used to let code run without halting on errors, letting all errors be reported after the code has been executed.
Reduce hard-to-find errors that may sneak into your code by always following coding conventions such as explicitly ending statements by semicolon instead of relying on the semicolon insertion; by always using braces around the bodies of statements of the control structures (if, if..else, switch, while, do..while, for, for..in statements); by using parentheses instead of relying on operator precedence; by using consistent and verbose naming conventions; by using indentation and spacing consistently in a way that makes your source code easily readable; by avoiding automatic type casting through using explicit type casting or other methods to achieve the same effect; by using full syntaces where browsers allow shortcuts (this especially goes for ie), etc.
Run the code through js lint, which will do some work towards detecting potential coding errors.
Ok, that was what you could do in the code itself. How about the detection of errors in the code?
Use many browsers for testing your scripts while you develop them. On windows, use at least ie6w, op7 and moz. On mac, use at least saf, op7, ie5m and moz. If things doesn't work in one or more of these browsers, see if you can do them differently. If not, make a fork for the chosen browser.
In ie, be sure to turn error reporting on. If you're on windows, use the Microsoft Script Debugger. You may use the debugger keyword inside the script to turn control of the execution of the script over to the debugger, if you need to track an error down. It's recommended that you use ie primarily for testing, and use op7 or moz for debugging.
In konq your are pretty much on your own. Use the source code tricks.
In saf you can turn on the hidden debug menu, to display frighteningly unhelpful error messages in the system Console, as well as get access to a more useful Show DOM Tree feature, if you turn on display of the Debug menu using the following command in the terminal while Safari is not running:
Q: How do you read, write, delete and detect support for cookies?
A: Let's take it one step at a time:
Writing cookies is more complicated. The easiest form of cookies are session-only cookies, which only lives as long as the browser window. These are written simply like this:
Deleting cookies is done by overwriting a cookie, setting the expiry time in the past. Thus,
where path and domain MUST be the same as they were when the cookie was originally set.
Detecting cookie support is done by writing a cookie and then reading it out again. If the value is the same as what you wrote, the browser supports cookies.
If you don't feel like writing your own cookies handling script, there are many cookies libraries out there that you can use. I personally wrote two different ones in this thread, for instance, and the rest of the CF community has contributed to among other places this thread.
Q: How do I format a number so that it always has two decimals?
A: There are more than one way to do it. You could use the Number.prototype.toFixed method to do it, if it wasn't for the fact that ie5.5w is buggy and saf doesn't support it at all. Instead, do something like this:
This code extends all numbers to contain a toDecimals method, which you can invoke like this:
sYourFormattedNumber=nYourNumber.toDecimals(2); // => '300.30'
Q: How do I get multiple scripts to work on a single page?
A: The short (not recommend) answer is to rename all of the variable and function names in the second instance of the script thus preventing them from conflicting.
The longer (recommend) answer would be to rewrite the script in an object-oriented fashion. The reason you'd do it like this is because this is inherently how object-oriented code is intended to work: with multiple instances. This is true because when you call the constructor for that object, all variables become internal properties of it therefore completely removing the chance of anything conflicting between the two scripts. Also note by making it an object you can have as many instances of it on a page as you'd like — it's not only limited to two.
For more information on this, check the following threads:
A. Short answer, you can't.
Last edited by Roy Sinclair; 03-15-2004 at 07:36 PM..
A: There are a number of 'tricks' you can use to do this.
Take this code:
var a=prompt("Enter number","");
var b=prompt("Enter number","");
As if() statements always return either true or false, you can just do this:
var a=prompt("Enter number 2","");
var b=prompt("Enter number 2","");
This is alerting the result of the test, which is (a==b).
var a=prompt("Enter number 3","");
var b=prompt("Enter number 3","");
alert("Enter a number")
As you can see, this tests whether two inputted numbers are equal, alerting a different message depending on:
A) If they don't enter two numbers
B) If the numbers are equal
C) If the numbers are not equal.
As in each if(), else if(), else statement, we are doing the same thing (alerting) but alerting something different we can use this:
var a=prompt("Enter number 4","");
var b=prompt("Enter number 4","");
[B]alert((!a||!b)?"Enter a number":(a==b)?"Equal":"Not equal");[/B]
Note the line in bold. This is how we write it:
First we start with alert(. We then put our first condition (inside the first if()) in brackets. So:
We then put a question mark after, to show it's an if() statement. We then put the text to alert if this condition is true, so:
alert((!a||!b)?"Enter a number"
Instead of else, we put a colon : . We then repeat what we have already done. So:
alert((!a||!b)?"Enter a number":(a==b)?"Equal"
The bit in bold is the condition for the else if() statement. The bit after is the resulting alert. If there are more else if() statements, we put a colon, and repeat. When we get to the last else, we just put a colon and the text to alert if the previous if() and else if()s were all false. In this case, we put a bracket to close the alert() function. We end up with:
alert((!a||!b)?"Enter a number":(a==b)?"Equal":"Not equal");
In if() statements or similar, we never need to use the following:
Instead of the first, we just need the a. So:
Instead of the second, we put an ! in front of the variable. So:
var d=new Date();
//repeat right up until:
As we are testing a variable against numbers 0-11, we can use an array to store all our months:
var d=new Date();
All the months are stored in an array named months. We refer to each item like this:
months for the first
months for the second
months for the third etc.
Instead of the number, we use d.getMonth() inside the square brackets, as this returns the number of the item we want.