You name it. Here is a list of various javascript resources I've found useful. Unless you are a Javacript Jedi Level 10 you will likely find the following resources very helpful. So dig in!
"Javascript is one of the worlds most misunderstood languages." - Douglas Crockford
JavaScript for an image gallery
// The default behavior on the link is intercepted to instead run a function.
// showPic - Swap out placeholder image by changing src attribute. Also changes the description text.
// It is the name for the argument which is passed into the showPic function. whichPic represents an element node. Example Images
- Using the setAttribute method to change the img src.
- Extract href path using the getAttribute method and store in a variable
CSS and JavaScript
Using JavaScript to manipulate CSS of something on the page.
Anything that would be written with a dash or hyphen becomes camel case. (Because we cant put a dash in code thats how we subtract)
I needed some style
Change an image onclick
JavaScript Panther Annimation
Using JavaScript to make that kitty run! Meeeow! Click the kitty to make it run.
Form and Form Elements
Retrieve the form or any field in a form:
document.forms.NameOfTheForm (the form)
document.forms.NameOfTheForm.nameOfTheElement (field element in a form)
onfocus - into it
onblur - when you leave the element
onchange - When something changes
onkeypress-
onkeydown -
onkeyup -
Required example e-mail field:
Timers
Just happens once.
Alert should appear after 2 seconds.
function simpleMessage() {
alert("This is just an alert box");
}
var timeMe = document.getElementById("timerButton");
timeMe.onclick = function() {
setTimeout(simpleMessage, 3000);
}
Set Interval
Change an element (image in this example) using setInterval (repeating at an interval you define).
icon_happy.png
icon_sad.gif
var myImage = document.getElementById("sadIcon");
var imageArray = ["icon_sad.gif", "icon_happy.png", "silly-icon.png", "crazy-smiley.jpg"];
var imageIndex = 0;
function changeImage() {
console.log('changeImage fired');
myImage.setAttribute("src", imageArray[imageIndex]);
imageIndex++;
if(imageIndex >= imageArray.length) {
imageIndex = 0;
}
}
setInterval(changeImage,1000);
Can you HANDLE it? - Handling or registering events
Events are happening all the time. You decide which ones you care about. When the page loads, when a button is clicked, when the user types, etc. Here is some info on how to handle Javascript events. Common Events - onload, onclick, onmouseover, onblur, onfocus.
Example - Write Jascript directly into your html. You should avoid script mixed in with your html.
Example - Name of the element, name of the event. Annonymous function that executes as soon as event happens:
var myImage = document.getElementById("mainImage");
myImage.onclick = function() {
// Your event handler code
// When user clicks this element run this function
// Semi colon because whole thing is a statement
};
Click the image -
Changing DOM Elements
Some examples of how you can change DOM elements using JavaScript.
Example Div/Element
Creating DOM elements:
Create the element
Add the newly created element to the right place in the DOM
First Item
Second Item
Regular Expressions
Regular Expressions are a tool and something to use when you need them. Regular Expressions are sequences of characters that can describe and match patterns in strings. Basically they do verification of patterns. A regular expression defines a pattern that can be searched for in a string. This pattern is stored in a regular expression object.
// Defines a very basic regular expression - Looks for the word hello somewhere in a string
var myRegExpression = /hello/;
var myString = "Testing for the word hello in a string."
if (myRegExpression.test(myString) ) {
alert("Yes");
}
Validation and Right Whitespace Trim
Event Bubbling Example
Through event bubbling an onclick event bubbles up to the topmost
object of the browser. This means that any event handlers defined for
the higher level objects are also activated. The event bubble can be
stopped, using event.cancelBubble. Learn more about
event
bubbling
and
event
capturing
.
Here is an excellent link to learn everything you need about javascript
pop ups. - YourHTMLSource If you want to use CSS to create
your popups check out css pop up
examples page.
Javascript For Back to Previous Page - Back
Button Back
JS Lint
Excellent for testing out your JavaScript code. It is very strict, it will always complain about your code but thats a good thing. Settings are at the bottom. Recommended settings to check are:
Javascript Links & Learning Resources:
JavaScript has been around forever and unfortunately there are a lot of bad JS resources out there so be careful. Below are some quality resources.
Mozilla Developer Network
Great community of web developers and a great resource for learning JavaScript.
Yahoo Developer Center - also see Yahoo JS Performance Center
Yahoo JavaScript Developer Center. Focused on people using Javascript libraries but a lot of great stuff in there.
Google Closure Compiler (Minifyer)
The Closure Compiler is a tool for making JavaScript download and run faster. It is a true compiler for JavaScript. Instead of compiling from a source language to machine code, it compiles from JavaScript to better JavaScript. It parses your JavaScript, analyzes it, removes dead code and rewrites and minimizes what's left. It also checks syntax, variable references, and types, and warns about common JavaScript pitfalls.
Great
Javascript Step by Step Tutorial
A great resource for anyone looking to learn advanced JavaScript. Takes
you step by step and shows you how things work.
Javascript
Ninja
A great book for all aspiring Javascript Ninjas. (Thanks Josh - U =
MyHero)
Syntax Highlighter
SyntaxHighlighter is a fully functional self-contained code syntax
highlighter developed in JavaScript. To get an idea of what
SyntaxHighlighter is capable of, have a look at the demo page.
JQuery
jQuery is a new kind of JavaScript Library. jQuery is a fast and
concise JavaScript Library that simplifies HTML document traversing,
event handling, animating, and Ajax interactions for rapid web
development. jQuery is designed to change the way that you write
JavaScript.
Moo Tools
MooTools is a compact, modular, Object-Oriented JavaScript framework
designed for the intermediate to advanced JavaScript developer. It
allows you to write powerful, flexible, and cross-browser code with its
elegant, well documented, and coherent API. Check out their Demos!