Download presentation
Presentation is loading. Please wait.
Published byHilary Nash Modified over 8 years ago
1
Web Programming Java Script Core Language Reference
2
The String Object Character Conversions Character Codes function showCharCode() { var theText = “”; if (window.getSelection) { theText = window.getSelection().toString(); } else if (document.getSelection) { theText = document.getSelection(); } else if (document.selection && document.selection.createRange) { theText = document.selection.createRange().text; } if (theText) { document.forms[0].charCodeDisplay.value = theText.charCodeAt(); } else { document.forms[0].charCodeDisplay.value = “ “; } function showString(form) { form.result.value = String.fromCharCode(form.entry1.value,form.entry2.value,form.entry3.value); }
3
The String Object document.onmouseup = showCharCode; Capturing Character Codes Select any of this text, and see the character code of the first character. Character Code:<input type=”text” name=”charCodeDisplay” size=”3” /> Converting Codes to Characters Enter a value 0-255: <input type=”button” value=”Show String” onclick=”showString(this.form)” /> Result:<input type=”text” name=”result” size=”5” />
4
The String Object Regular Expression Match Workshop Regular Expression Match function doMatch(form) { var str = form.entry.value; var delim = (form.caseSens.checked) ? “/g” : “/gi”; var regexp = eval(“/” + form.regexp.value + delim); var resultArray = str.match(regexp); if (resultArray) { form.result.value = resultArray.toString(); form.count.value = resultArray.length; } else { form.result.value = “ ”; form.count.value = “”; }
5
The String Object String Match with Regular Expressions Enter a main string:<input type=”text” name=”entry” size=”60” value=”Many a maN and womAN have meant to visit GerMAny.” /> Enter a regular expression to match:<input type=”text” name=”regexp” size=”25” value=”\wa\w” /> <input type=”checkbox” name=”caseSens” />Case-sensitive <input type=”button” value=”Execute match()” onclick=”doMatch(this.form)” /> Result: Count:
6
The String Object Lab for string.replace() and string.search() Regular Expression Replace and Search var mainString = “To be, or not to be: that is the question:\n”; mainString += “Whether \’tis nobler in the mind to suffer\n”; mainString += “The slings and arrows of outrageous fortune,\n”; mainString += “Or to take arms against a sea of troubles,\n”; mainString += “And by opposing end them.”; function doReplace(form) { var replaceStr = form.replaceEntry.value; var delim = (form.caseSens.checked) ? “/g” : “/gi”; var regexp = eval(“/” + form.regexp.value + delim); form.result.value = mainString.replace(regexp, replaceStr); } function doSearch(form) { var replaceStr = form.replaceEntry.value; var delim = (form.caseSens.checked) ? “/g” : “/gi”; var regexp = eval(“/” + form.regexp.value + delim); form.result.value = mainString.search(regexp); } String Replace and Search with Regular Expressions
7
The String Object Text used for string.replace() and string.search() methods: To be, or not to be: that is the question: Whether ‘tis nobler in the mind to suffer The slings and arrows of outrageous fortune, Or to take arms against a sea of troubles, And by opposing end them. Enter a regular expression to match:<input type=”text” name=”regexp” size=”25” value=”\B’t” /> <input type=”checkbox” name=”caseSens” />Case-sensitive Enter a string to replace the matching strings:<input type=”text” name=”replaceEntry” size=”30” value=”it “ /> <input type=”button” value=”Execute replace()” onclick=”doReplace(this.form)” /> <input type=”button” value=”Execute search()” onclick=”doSearch(this.form)” /> Result:
8
The String Object Using Simple String Methods HTML by JavaScript var page = “”; page += “JavaScript can create HTML on the fly. Numerous string object methods facilitate creating text that is “ + “boldfaced”.bold() + “, “ + “italicized”.italics() + “, or even the terribly annoying “ + “blinking text”.blink() + “.”; document.write(page);
9
The Math, Number, and Boolean Objects Decimal-to-Hexadecimal Converter Function function toHex(dec) { hexChars = “0123456789ABCDEF”; if (dec > 255) { return null; } var i = dec % 16; var j = (dec - i) / 16; result = “0X”; result += hexChars.charAt(j); result += hexChars.charAt(i); return result; }
10
The Math, Number, and Boolean Objects When a number isn’t a number var ageEntry = parseInt(document.forms[0].age.value); if (isNaN(ageEntry)) { alert(“Try entering your age again.”); } JavaScript Math Properties
11
The Math, Number, and Boolean Objects
12
The Array Object A Simple Parallel Array Lookup Parallel Array Lookup // the data var regionalOffices = new Array(“New York”, “Chicago”, “Houston”, “Portland”); var regionalManagers = new Array(“Shirley Smith”, “Todd Gaston”, “Leslie Jones”, “Harold Zoot”); var regOfficeQuotas = new Array(300000, 250000, 350000, 225000); // do the lookup into parallel arrays function getData(form) { var i = form.offices.selectedIndex; form.manager.value = regionalManagers[i]; form.quota.value = regOfficeQuotas[i]; }
13
The Array Object Parallel Array Lookup Select a regional office: <select name=”offices” onchange=”getData(this.form)”> New York Chicago Houston Portland The manager is: The office quota is: <input type=”text” name=”quota” size=”8” />
14
The Array Object Array Object Methods:Array Concatenation Array Concatenation // global variables var arrayOne, arrayTwo, arrayThree, textObj; // initialize after load to access text object in form function initialize() { var form = document.forms[0]; textObj = form.original; arrayOne = new Array(“Jerry”, “Elaine”,”Kramer”); arrayTwo = new Array(“Ross”, “Rachel”,textObj); arrayThree = arrayOne.concat(arrayTwo); update1(form); update2(form); showArrays(); } // display current values of all three arrays function showArrays() { var form = document.forms[0]; form.array1.value = arrayOne.join(“\n”); form.array2.value = arrayTwo.join(“\n”); form.array3.value = arrayThree.join(“\n”); }
15
The Array Object // change the value of first item in Array Three function update1(form) { arrayThree[0] = form.source1.value; form.result1.value = arrayOne[0]; form.result2.value = arrayThree[0]; showArrays(); } // change value of object property pointed to in Array Three function update2(form) { arrayThree[5].value = form.source2.value; form.result3.value = arrayTwo[2].value; form.result4.value = arrayThree[5].value; showArrays(); } table> arrayOne arrayTwo arrayThree
16
The Array Object Enter new value for arrayThree[0]: <input type=”text” name=”source1” value=”Jerry” /> <input type=”button” value=”Change arrayThree[0]” onclick=”update1(this.form)” /> Current arrayOne[0] is: Current arrayThree[0] is: textObj assigned to arrayTwo[2]:<input type=”text” name=”original” onfocus=”this.blur()” /> Enter new value for arrayThree[5]: <input type=”text” name=”source2” value=”Phoebe” /> <input type=”button” value=”Change arrayThree[5].value” onclick=”update2(this.form)” /> Current arrayTwo[2].value is:<input type=”text” name=”result3” /> Current arrayThree[5].value is: <input type=”button” value=”Reset” onclick=”location.reload()” />
17
Control Structures and Exception Handling Using try-catch-finally Constructions function attachToEnd(theNode, newTag) { try { var newElem = document.createElement(newTag); theNode.appendChild(newElem); } catch (e) { switch (e.name) { case “INVALID_CHARACTER_ERR” : statements to handle this createElement() error break; case “HIERARCHY_REQUEST_ERR” : statements to handle this appendChild() error break; case “WRONG_DOCUMENT_ERR” : statements to handle this appendChild() error break; case “NO_MODIFICATION_ALLOWED_ERR” : statements to handle this appendChild() error break; default: statements to handle any other error } return false; } return true; }
18
Control Structures and Exception Handling Throwing String Exceptions Throwing a String Exception var letters = new Array(“A”,”B”,”C”,”D”,”E”); function getLetter(fld) { try { var inp = parseInt(fld.value, 10); if (isNaN(inp)) { throw “Entry was not a number.”; } if (inp 5) { throw “Enter only 1 through 5.”; } fld.form.output.value = letters[inp - 1]; } catch (e) { alert(e); fld.form.output.value = “”; fld.focus(); fld.select(); } Throwing a String Exception Enter a number from 1 to 5: <input type=”text” name=”input” size=”5” /> <input type=”button” value=”Get Letter” onclick=”getLetter(this.form.input)” /> Matching Letter is:<input type=”text” name=”output” size=”5” />
19
Functions and Custom Objects A Function’s arguments and caller Properties function hansel(x,y) { var args = hansel.arguments; document.write(“ hansel.caller is “ + hansel.caller + “ ”); document.write(“hansel.arguments.length is “ + hansel.arguments.length + “ ”); for (var i = 0; i < args.length; i++) { document.write(“argument “ + i + “ is “ + args[i] + “ ”); } document.write(“ ”); } function gretel(x,y,z) { today = new Date(); thisYear = today.getFullYear(); hansel(x,y,z,thisYear); } hansel(1, “two”, 3); gretel(4, “five”, 6, “seven”);
20
Functions and Custom Objects Calling a Generalizable Function Variable Scope Trials function factorial(n) { if (n > 0) { return n * (factorial(n - 1)); } else { return 1; } Enter an input value: <input type=”button” value=”Calc Factorial” onclick=”this.form.output.value = factorial(this.form.input.value)” /> Results:
21
Thanks
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.