![]() |
JavaScript - Frequently Asked Questions
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.
Index:
Browsers:
|
Q: How do I debug JavaScript as used on a webpage?
A: There are many approaches that you can take to debugging JavaScript. Let's discuss what you may do in the code itself first:
|
Q: How do you read, write, delete and detect support for cookies?
A: Let's take it one step at a time:
|
Q: What are the limits on cookies?
A: There's a list of limits put on cookies:
|
Q: What is Javascript good for?
A: Because any general web user may have javascript in their browser disabled any use of javascript on a web page is best limited to enhancing the functionality, user friendliness and overall experience on your web pages. Nothing on your page should absolutely depend on javascript unless it's a non-essential part of the page. Javascript can also be used to make a page less friendly but there's no point in doing that. Useful things: Forms Validation, because Javascript can be disabled you must always perform validation on the server side but any validation of user input you can also perform interactively with the user before a form is submitted can save the user a round trip to the server and can save your server a hit where no actual transaction occurs. 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. Visual aides, the Title property can and should be used to give a user of your web page additional information about some element or group of elements on your page but javascript can be used to supplement the relatively weak content control available via the title property with a much richer full html content using a tooltip script. |
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:
Code:
Number.prototype.toDecimals=function(n){Code:
var |
Q: How do I trim whitespace from the beginning and end of a string?
A: By using regular expressions matching:
Code:
String.prototype.trim=function(){Code:
var |
Q: Why does parseInt('08') generate 0?
A: Because JavaScript integers are automatically radix detected. A number starting with 0 is considered to be octal, a number starting with 0x or 0X is considered to be hexadecimal, all other numbers are considered to be decimal. In octal, the digits range from 0 to 7, and thus the 8 is an illegal digit.
How to fix it? That is easy. You can override the JavaScript automatic radix detection by providing a second argument to the parseInt function, having a value of 10 (for decimal numbers). Thus, you do it like this: Code:
var nYourNumber=parseInt('08', 10); |
Q: How do I read/write files?
A: Pure client-side JavaScript cannot write files to the server because it simply was never intended to and moreover, that's what server-side languages are used for. Perhaps there are security reasons among others for this as well. As for reading files with JavaScript, this is completely possible and done quite easily if you use something like Vlad's script or David's script. |
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: |
Q: How do I convert a decimal number to hexadecimal, and the other way around?
A: For converting a hexadecimal number to a decimal number, you use the parseInt function with a radix of 16, like this:
Code:
var nDecimal=parseInt(sHexadecimal, 16);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: Code:
var sHexadecimal=nDecimal.toString(16); |
Q: How do I add ordinals (st, nd, rd, th) to a number?
A: After quite some optimisation, you could end up with a function looking something like this:
Code:
Number.prototype.toOrdinal=function(m){Code:
var |
Q: How do I use JavaScript in external files?
A: This is a question that is seldom answered satisfactory. Well, first of all, you should not include any HTML in your JavaScript file. This means that you should not include things like <script type="text/javascript"><!-- or --></script> in the file. Those belong in the HTML file only.
Now, the exact same code should behave exactly the same whether it's included in the document or in a separate file. However, there is one issue you must be aware of with JavaScript files: If the JavaScript is placed in the head of the document, the files will be downloaded as soon as the HTML parser reaches them, and they will be executed as soon as they are finished downloading. This means that you can not rely on them being executed in the order they appear in the head. To get around this, you can make sure no code that should actually execute is present in the JavaScript file, only variable declarations and functions. Then you move the execution of code into the window.onload function where you can rely on all of the JavaScript files being referenced in the head to be loaded. Note that this goes only for scripts included in the head of the document, not scripts in the body of the document. |
Q. How can I use Javascript to protect my web pages?
A. Short answer, you can't. Long Answer: Since visitors to your web site can disable Javascript in their browser, javascript can't be used as a protection mechanism. Even without disabling Javascript I can easily see the whole code for your web page by simply running another javascript which will undo anything you did to make the page unavailable. Search for "Bookmarklets" in Google for a bunch of simple but useful scripts like that which can be set up as Bookmarks (Favorites for you IE users). Please note that "No Right Click" scripts (one of the favorite uses of javascript to "protect" a web page) are considered at best a "waste of time" but more often as "user antagonistic" by most of the regulars here. You can get answers on how to write them, fix them and the like eventually but you may find yourself running a gauntlet of questions about why you would want to do such a thing before you get assistance. Scripts that make it harder to provide help do not endear you to the volunteers who provide the help. |
Q: How do I shorten my lengthly if() statements?
A: There are a number of 'tricks' you can use to do this.
1: Take this code:2: 3: In if() statements or similar, we never need to use the following:4: Take this: |
| All times are GMT +1. The time now is 05:38 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.