Useful tools
Firefox:- Has a built in error console that is very useful for debug purposes. To access it go to: "Tools" -> "Error Console";
- The add-ons "FireBug" and the "Web Developer tool bar" are tools that can come in handy for big applications;
You can use a simple text editor to write JavaScript but some features are very useful:
- line numbers (the Firefox "error console" reports on line numbers);
- code folding lets you fold up and down portions of code (an entire function or html table for example) and also help checking for incorrect open/close tags;
- syntactical support like syntax coloring;
Some good, also freeware, IDEs that have this features and others like "code completion" are: NetBeans and Visual Web Developer.
Use the "var" keyword to distinguish between global and local variables
<script type="text/javascript">
xpto = "I'm global";
function scopeDemo()
{
var xpto = "Actually, I'm local!";
}
scopeDemo();
document.write(xpto);
</script>Output: "I'm global"Instead if we omit the "var" keyword from
var xpto = "Actually, I'm local!"; The output would be: "Actually, I'm local!"Note: Has a best practice always use the "var" keyword when creating a new variable.
Javascript in external file
<script type="text/javascript" src="xxx.js"></script>Strings
In JavaScript, strings are implemented as objects. So, when you create a string, you are actually creating a JavaScript object, a String object.W3Schools have a great list of the String properties and methods.
Arrays
Like strings, JavaScript arrays are implemented as objects. Unlike strings, however, you can't just create an array by assigning a value to a variable. Instead you have to actually create an Array object:var myArray = new Array();Numbered arrays
myArray[3] = 100.1;
myArray[4] = "Yeah!";
myArray[5] = 30;
Note:
- although numbered arrays usually begin at index 0, it doesn't have to be that way.
- distinct data types can be stored on the same array;
var myArray = new Array();
myArray["Price"] = 123.4;
myArray["Product Descciption"] = "A great product for something";
Note:
This named array could easily be iterated with the for each loop (in this case we are printing the index and the value):
for (x in myArray){
document.write(x + "is" + myArray[x]);
}Array methods and properties
Check a complete list at w3schools
Browser Object Model
- Window object represents the web browser window (or the HTML document loaded into a particular frame - achieved by the Document object of each window). If a document contain frames (<frame> or <iframe> tags), the browser creates one window object for the HTML document, and one additional window object for each frame. Keep in mind that a window object and its properties can be assigned to a JavaScript variable just like any other object (this can be used to track and open multiple windows);
- History object contains the URLs visited by the user (within a browser window);
- Navigator object contains information about the browser;
- Location object contains information about the current URL;
- Document object provides access to all HTML elements in a page, from within a script. Each HTML document loaded into a browser window becomes a Document object. Its methods and properties let you investigate and manipulate the HTML page;
- Screen object contains information about the visitor's screen;
Window Object
Some methods of this object include:- alert( message ) halts execution and displays a dialog containing a message that you specify along with a single OK button;
- confirm( message ) asks the user a simple "Ok" or "Cancel" question. "OK" button returns true;
- prompt( question, defaultValue ) ask the user a question, and get back an answer. The "Cancel" button returns null;
Note:
The window object can be omit when acessing most of the other objects ("window.document" is the same as "document").
See a complete list of properties and methods on w3schools;
No comments:
Post a Comment