Professional Documents
Culture Documents
doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="jstut.js"></script>
<style type="text/css">
body {font-size: 1.6em;}
.hidden {display:none;}
.show {display:inline !important;}
button {
border: 2px solid black; background: #E5E4E2;
font-size: .5em; font-weight: bold; color: black;
padding: .8em 2em;
margin-top: .4em;
}
</style>
</head>
<body>
<p id="sayHello"></p>
<script>
var randNum = 5;
// Order of operations
document.write("3 + 2 * 5 = ", 3 + 2 * 5, "<br/>");
document.write("(3 + 2) * 5 = ", (3 + 2) * 5, "<br/>");
// String length
document.write("String Length : ", randStr.length + "<br/>");
// Replace a string
document.write(randStr.replace("and on", "forever") + "<br/>");
// Convert to uppercase
document.write(randStr.toUpperCase() + "<br/>");
// Convert to lowercase
document.write(randStr.toLowerCase() + "<br/>");
// Styling with JS
var strToStyle = "Random String";
document.write("Big : ", strToStyle.big(), "<br />");
document.write("Bold : ", strToStyle.bold(), "<br />");
document.write("Font Color : ", strToStyle.fontcolor("blue"), "<br />");
document.write("Font Size : ", strToStyle.fontsize("8em"), "<br />");
document.write("Italics : ", strToStyle.italics(), "<br />");
document.write("Google : ", strToStyle.link("http://google.com"), "<br />");
document.write("Small : ", strToStyle.small(), "<br />");
document.write("Strike : ", strToStyle.strike(), "<br />");
document.write("Sub : ", strToStyle.sub(), "<br />");
document.write("Sup : ", strToStyle.sup(), "<br />");
var age = 8;
case 7 :
document.write("Go to 1st Grade<br />");
break;
default :
document.write("Subtract 5 from your age<br />");
}
// do while is used when you must go through the loop at least once
do{
var guess = prompt("Guess a number between 1 and 20");
}while(guess != 15)
var customer = {name : "Bob Thomas", address : "123 Main", balance : 50.50};
// Delete an index
delete tomSmith[3];
// Sort numbers
var numbers = [4,3,9,1,20,43];
// Combine arrays
var combinedArray = numbers.concat(tomSmith);
function times2(num){
var var2 = 2;
return num * var2;
}
// Recursive Function
function factorial(num){
if(num <= 1){
return 1;
} else {
return num * factorial(num - 1);
}
}
</script>
<!-- Get value in an input element and open alert on change -->
<input type="text" id="randInput"
onChange="var dataEntered=document.getElementById('randInput').value; alert('User
Typed ' + dataEntered);"><br /><br />
<!-- When a user clicks a key provide info on the key clicked -->
<form action="#" id="sampForm">
<input id='charInput' type="text">
<p id="keyData">Key Data Here</p>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form><br /><br />
<script>
function getChar(event) {
document.getElementById('charInput').onkeypress = function(event) {
var char = getChar(event || window.event)
if (!char) return false; // Special Key Clicked
function browserResized() {
document.getElementById('keyData').innerHTML = "I've been resized";
}
document.getElementById('mouseX').value = pageX;
document.getElementById('mouseY').value = pageY;
};
</script>
<script type="text/javascript">
<script type="text/javascript">
// You can get all ps and then target them like an array
var pElements = document.getElementsByTagName('p');
pElements[3].style.backgroundColor = "#EFDECD";
sampDiv2.childNodes[1].childNodes[3].style.backgroundColor = "#BFAFB2";
// Change attribute
nttLogo2.setAttribute("alt", "NTT Logo 2");
// Get attribute
document.write("Logo alt Value : ", nttLogo2.getAttribute("alt"), "<br />");
paragraph3.setAttribute("id", "paragraph3");
sampDiv2.appendChild(paragraph3);
</script>
<!-- ---------- OO JAVASCRIPT ---------- -->
<script type="text/javascript">
// Delete a property
delete cust1.country;
this.payDownBal = function(amtPaid){
this.balance -= amtPaid;
};
this.addToBal = function(amtCharged){
this.balance += amtCharged;
};
}
cust2.addToBal(15.50);
document.write(cust2.toString());
</script>
<div>
Enter your name:
<!-- When they leave the input send a reference to the input element, and a reference to the
hel error span -->
<input id="name" name="name" type="text" size="30" onblur="isTheFieldEmpty(this,
document.getElementById('name_help'))" />
<span id="name_help"></span>
<!-- this is the id number for the text box -->
</div>
<div>
Enter your street address:
<input id="street" name="street" type="text" size="30" onblur="isAddressOk(this,
document.getElementById('street_help'))" />
<span id="street_help"></span>
</div>
<div>
Enter your city:
<input id="city" name="city" type="text" size="30" onblur="isTheFieldEmpty(this,
document.getElementById('city_help'))" />
<span id="city_help"></span>
</div>
<div>
Enter your state code:
<input id="state" name="state" type="text" size="2" onblur="isStateOk(this,
document.getElementById('state_help'))" />
<span id="state_help"></span>
</div>
<div>
Enter your phone number:
<input id="phone" name="phone" type="text" size="15"
onblur="isPhoneOk(this, document.getElementById('phone_help'))" />
<span id="phone_help"></span>
</div>
<div>
Enter your email:
<input id="email" name="email" type="text" size="30" onblur="isEmailOk(this,
document.getElementById('email_help'))" />
<span id="email_help"></span>
</div>
<script type="text/javascript">
{
// See if the info matches the regex that was defined
// If the wrong information was entered, warn them
if (!regex.test(input)) {
if (helpId != null)
// We need to show a warning
// Remove any warnings that may exist
while (helpId.childNodes[0]){
helpId.removeChild(helpId.childNodes[0]);
}
} else {
}
}
return editNodeText(/^A[LKSZRAEP]|C[AOT]|D[EC]|F[LM]|G[AU]|HI|I[ADLN]|K[SY]|
LA|M[ADEHINOPST]|N[CDEHJMVY]|O[HKR]|P[ARW]|RI|S[CD]|T[NX]|UT|V[AIT]|
W[AIVY]$/, inputField.value, helpId, "Enter a State Code in Uppercase (Ex.NY, PA, CA)");
}
</script>
<script type="text/javascript">
// Through exception handling we can catch and manage errors rather then
// crashing by surrounding problem code in a try block and handling it
// in a catch block
try {
document.write("Customer : ", getCust(5), "<br />");
}
catch(ex){
if (ex instanceof RangeError){
document.write(ex.message + "<br />");
}
}
</script>
</body>
</html>
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Object Oriented JavaScript</title>
</head>
<body>
<script type="text/javascript">
// A basic JavaScript object with properties and a method
var customer = {
name: 'Tom Smith',
speak: function(){
function SecretCode(){
// This value can't be accessed directly
var secretNum = 78;
// Returns undefined
document.write("Value of secretNum : " + secret.secretNum + "<br />");
Circle.prototype = {
set radius(radius) { this._radius = radius; },
get radius() { return this._radius; },
get area() { return Math.PI * (this._radius * this._radius); }
};
var circ = new Circle(10);
circ.radius = 15;
document.write("A circle with radius " + circ.radius + " has an area of " +
circ.area.toFixed(2) + "<br />");
function Animal(){
this.name = "Animal";
function Canine(){
this.name = "Canine";
}
function Wolf(){
this.name = "Wolf";
}
Animal.prototype.getSound = function(){
return this.name + " says " + this.sound;
}
Canine.prototype.sound = "Woof";
Wolf.prototype.sound = "Grrrr Wooof";
// More often then not it makes more sense to just inherit the
// prototype to speed up the lookup process
function Rodent(){
this.name = "Rodent";
}
function Rat(){
this.name = "Rat";
}
Temp.prototype = Parent.prototype;
Child.prototype.constructor = Child;
function Deer(){
this.name = "Deer";
this.sound = "Snort";
}
extend(Deer, Animal);
function Truck(name) {
this.name = name
}
// If it does return it
return Hero.instance;
}
return this;
}
function Sword(desc){
this.weaponType = "Sword";
this.metal = desc.metal || "Steel";
this.style = desc.style || "Longsword";
this.hasMagic = desc.hasMagic || false;
}
function Bow(desc){
this.weaponType = "Bow";
this.material = desc.material || "Wood";
this.style = desc.style || "Longbow";
this.hasMagic = desc.hasMagic || false;
}
function WeaponFactory(){};
WeaponFactory.prototype.makeWeapon = function(desc){
var weaponClass = null;
Pizza.prototype.getPrice = function(){
return this.price;
}
function ExtraCheese(pizza){
var prevPrice = pizza.price;
pizza.price = prevPrice + 1;
}
ExtraCheese(myPizza);
Observable.prototype = {
subscribe: function(subscriber) {
// Add the subscriber object to the list
this.subscribers.push(subscriber);
},
unsubscribe: function(unsubscriber) {
var OrganFanny = {
name: "Organ Fanny",
receiveData: function(data){
document.write(this.name + " received your info : " + data + "<br />");
}
}
var BoldmanYaks = {
name: "Boldman Yaks",
receiveData: function(data){
document.write(this.name + " received your info : " + data + "<br />");
}
}
observable.publish('IBM at $145.33');
</script>
</body>
</html>