JavaScript Tips

JavaScript comments are done with two forward slashes

// This is a JavaScript Comment

/*

This is a much longer JavaScript Comment
It spans TWO lines
or THREE lines

*/

(Just like PHP comments)

<!--      --> HTML Comment

Semicolons

Statements (a single line of code) are usually ended with a semicolon, although they are not required to. If doing two instructions on the same line, THEN, semicolon is required.

It is case sensitive.

When naming variables, be sure to remember HOW the case is...common cases are all lowercase or camelcase...ex. camelcase vs. camelCase

ALSO, when using predefined functions or keywords, remember that they are also case Sensitive

(ex. parseint is NOT the same as parseInt...The first one doesn't work at all; the second one does;-) )

(BTW, parseInt takes a string (from a text field) and converts it to an Integer. parseFloat does the same thing, but turns it into a decimal number)

JS ignores any extraneous whitespace

Other than the initial space separating words, JS ignores any extraneous whitespace. This means that you can insert additional lines to make your code more readable for humans.

Ex. x+z=y


is the exact same as
x + z = y

(or as I like to say: x+z=y as in y do I care???)

Three Different Ways to Use JavaScript

Can embed the script between a <script> tag and closing </script> tag OR can link to a different file OR can place within an HTML tag

Ex.
FIRST WAY
Embedded in HTML file
<script src="jstest.js">
alert("Do you see the page heading?");

</script>

SECOND WAY
Different file

<script src="jstest.js"></script>

jstest.js file contents


alert("Do you see the page heading?");

--note that there are NO script tags ANYWHERE
(just like a .css file...NO HTML tags etc....)

A good reason for including the .js file instead of embedding it, is to be able to reuse code...always a good idea!!

THIRD WAY
The third way to use it, is to embed it within the HTML tag. This is only done in the case of event handlers (a much more advanced topic)

Cookies

Special Characters

HTML to JavaScript String Methods

Language Comparisons

Java

JavaScript

Images

 

URLs

Opening a New Document Window

<FORM>
<INPUT type="button" value="New Window!" onClick="window.open('http://www.pageresource.com/jscript/jex5.htm','mywindow','width=400,height=200')">
</FORM>

Disabling Shared Cookies in the IE4 browser (confirm this...)

Go to Tools > Internet Options, and then click the Security tab.
Click Internet, and then click Custom Level.
Scroll down to Cookies, click disable (or enable) for both cookie options, and then click OK.

Disabling Shared Cookies in the IE6 browser

Go to Tools > Internet Options, and then click the Privacy tab.
Click Advanced, and then click Override Automatic Cookie Handling, then choose your options

cookie settings in IE 6


Here's a GREAT little script that disables Right Clicking and saving your images

Here's an example of my Balances web application


Back to Class Tips

Back to Home