||03-01-2004 08:15 PM
If you have input on the questions already in the FAQ or want to suggest a new question for someone to write an answer to, please post here
- How do you read, write, delete and detect support for cookies?
- What are the limits on cookies?
- How do I format a number so that it always has two decimals?
- How do I trim whitespace from the beginning and end of a string?
- Why does parseInt('08') generate 0?
- How do I read/write files?
- How do I get multiple scripts to work on a single page?
- How do I convert a decimal number to hexadecimal, and the other way around?
- How do I add ordinals (st, nd, rd, th) to a number?
- How do I shorten my lengthly if() statements?
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.
Microsoft Internet Explorer for Windows. <http://www.microsoft.com/windows/ie/default.asp>, Windows
Microsoft Internet Explorer for Mac. <http://www.microsoft.com/mac/product...ternetexplorer>, Mac
Apple Safari (based on KHTML and KJS by KDE) <http://www.apple.com/safari/>, Mac OS X
KDE Konqueror. <http://www.konqueror.org/>, X11
Opera. Normally we're talking about newest verion on Windows and Linux and Mac. <http://www.opera.com/download/>, Windows, Linux, Mac.
Netscape6, Netscape7, Netscape8. Netscape browser based on Mozilla.
Netscape Navigator. Netscape browsers up to version 4.x.
Netscape Communicator. Same as nn4.x.
||03-01-2004 08:17 PM
- 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:
defaults write com.apple.Safari IncludeDebugMenu 1
||03-01-2004 08:17 PM
Q: How do you read, write, delete and detect support for cookies?
: 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.
'expires=Thu, 01-Jan-70 00:00:01 GMT;'+
- 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
||03-01-2004 08:18 PM
Q: What are the limits on cookies?
: There's a list of limits put on cookies:
- Cookies are limited to one domain. There is a limit of 20 cookies per domain. Cookies can be read only from pages from that domain.
- Cookies can further be limited by paths, so that only pages with a specific path on that domain may read the cookie.
- Cookies have an expiry date. If no such date is set, they will only survive as long as the browser session is still running.
- Cookies that pass the expiry date are removed, but not necessarily from your harddrive. It depends on how your browser stores them.
- Cookies may be 4kb long each, name of the cookie included.
- Your max limit is no smaller than 300 cookies in total.
- Most browsers allow no longer expiry time than three years. Some have a 90 day limit even.
Official specifications exist for this, and can be found at Netscape Cookie Specification
, which is the base for the IETF RFC2109
||03-01-2004 10:03 PM
Interactive Forms, In the case of something like an online store it's always nice to update order totals and (when possible) shipping costs and other incidental costs (handling fees, taxes...) as the user updates the quantities or selects/deselects various items on the page. While you may want to post that toal back to your server for your own security, you must never trust that figure. Recalculate any totals based on the posted selections/quantities, you can however compare the posted total vs the server side computed total to detect bugs in the script and/or attempts at theft.
||03-01-2004 11:05 PM
Q: How do I format a number so that it always has two decimals?
: 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'
||03-01-2004 11:59 PM
Q: How do I trim whitespace from the beginning and end of a string?
: By using regular expressions matching:
These can then be used like this:
sOriginal=' text ',
/* After execution:
sOriginal is ' text '
sTrim is 'text'
sLTrim is 'text '
sRTrim is ' text'
||03-04-2004 08:25 AM
Q: Why does parseInt('08') generate 0?
is considered to be octal, a number starting with 0x
is considered to be hexadecimal, all other numbers are considered to be decimal. In octal, the digits range from 0
, and thus the 8
is an illegal digit.
function, having a value of 10
(for decimal numbers). Thus, you do it like this:
var nYourNumber=parseInt('08', 10);
For a more thorough explanation, see my textual explanation
or my tabular breakdown
||03-04-2004 08:34 AM
Q: How do I read/write files?
or David's script
||03-04-2004 08:35 AM
Q: How do I get multiple scripts to work on a single page?
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:
||03-04-2004 08:49 AM
Q: How do I convert a decimal number to hexadecimal, and the other way around?
: For converting a hexadecimal number to a decimal number, you use the parseInt
function with a radix of 16
, like this:
var nDecimal=parseInt(sHexadecimal, 16);
is a string containing a hexadecimal number.
For converting a decimal number to hexadecimal, you use the toString
method with an argument of the radix you want to convert to, in this case 16
, like this:
is either a variable name, a number literal wrapped in parentheses, or a function giving a number as return value.
||03-04-2004 09:46 AM
Q: How do I add ordinals (st, nd, rd, th) to a number?
: After quite some optimisation, you could end up with a function looking something like this:
return (this +
((m=this%10) >3) ||
and you use it like this:
sFirstOrdinal=nNumber.toOrdinal(), // => '12th'
sSecondOrdinal=(22).toOrdinal(); // => '22nd'
Well, I won't bore you with the development procedure of this very slick and optimised function. You can check it out yourself in this thread
||03-12-2004 02:04 PM
||03-15-2004 08:29 PM
A. Short answer, you can't.
||04-19-2004 08:03 PM
Q: How do I shorten my lengthly if() statements?
A: There are a number of 'tricks' you can use to do this.
Take this code:2:
As if() statements always return either true or false, you can just do this:
var a=prompt("Enter number","");
var b=prompt("Enter number","");
This is alerting the result of the test, which is (a==b).
var a=prompt("Enter number 2","");
var b=prompt("Enter number 2","");
As you can see, this tests whether two inputted numbers are equal, alerting a different message depending on:
var a=prompt("Enter number 3","");
var b=prompt("Enter number 3","");
alert("Enter a number")
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:
Note the line in bold. This is how we write it:
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]
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:4:
Instead of the first, we just need the a. So:
Instead of the second, we put an ! in front of the variable. So:
As we are testing a variable against numbers 0-11, we can use an array to store all our months:
var d=new Date();
//repeat right up until:
All the months are stored in an array named months. We refer to each item like this:
var d=new Date();
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.
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.