Where Do I Place the <script> Block?
It can go pretty much anywhere in the HTML of the page, but if you stick with it in the <head> for as much of the time as possible, you'll make your life easier because that way you guarantee that your script will be loaded before the page, and reduce errors.
What Kinds of Errors?
Take rollovers on a page. If you allow the page to load in before the script, users will get error messages when they roll over the hot imagewhich looks really bad and is really annoying!
You only really need those semicolons if you are stringing more than one statement on the same line; something like this (in which we have statements within event-handling attributes as part of a string):
<a href="http://www.kingsley-hughes.com/index.htm" onMouseOver="window.status='Click to visit our website!!!'; return true;" onMouseOut ="window.status=''; return true;">Click Here!</a>
Adding the semicolons at the ends of statements does make your code a lot more readable and easier to follow, so they might be worth adding. However, if you want less to type, just leave them out!
Help, I'm Having Problems Using Quotations in My Code!
When you want to use quotations, or in fact any other character that you are having problems with, you can prefix them with the backslash (\) character:
How Do You Get a Carriage Return in a Dialog Box?
Use \r at the point where you want the return.
What About On the Page, as When Using a document.write()?
In this case, you need to forget the \r and go with the HTML <br>.
0.119 * 100
To get the correct answer, you would need to round the output to three decimal places.
Can I Hide My Script from Prying Eyes?
Only if you want it to work in Internet Explorer! Microsoft has bought out a script encode that will encrypt your script yet still allow it to work.
Get the script encoder from: http://msdn.microsoft.com/scripting/vbscript/download/vbsdown.htm.
How Do I Round Numbers Off to a Certain Number of Decimal Places?
Use Math.round() to multiply the number by ten times the number of decimal places you want to round off by; then divide by that number.
For example, this will round off to tenths:
var pi = "3.1415926535"; pi = Math.round(pi*10)/10;
And this to hundredths:pi = Math.round(pi*100)/100;
You do not include opening and closing <script> tags in the .js file!
To embed script.js into your Web page, use these tags in your HTML file:
In addition to the previous list, here are other words you should avoid (they are the names of client-side objects, methods, or properties in Netscape Navigator or Internet Explorer):
To generate random floating-point numbers in the range from 0 to 1, use the Math.random() method:
num = Math.random()
If you need random floating-point numbers in the range from X to Y, use this code:
num = X + (Y-X)*Math.random()
I'm Worried About "Millennium Bugs" In My Code. What Can I Do?
You can use the following code snippet to correct any problems you might come across when retrieving the current year:
theDate = new Date(); theYear = theDate.getYear(); if (theYear<1900) theYear=theYear+1900;