Elderflowers/hi>
Hel10
friend!
class="action">Pay Nows/a>
@ erste savascnier mstRUcTIONS
Basic JavaScript Instructions ~EXAMPLE
BASIC JAVASCRIPT
INSTRUCTIONS
This example combines several techniques that
you have seen throughout this chapter.
You can se the code fr this example on the next two pages. Single line
comments ae used to describe what each section ofthe cade does.
‘Tostart, three variables are created that store informatio thats used
‘nthe weleome message. These variables ar then concatenated (ined
together to creat the fullmessagethe user sees
The next part ofthe example demonstrates how basic math is
performed on numbers to calculate the cost of sign.
'© A variable called sign holds the text the sgn will show.
1© A property called Tength is used to determine how many characters,
aein the string (you will meet his property on e128).
1 The cos ofthe sign (the subtotal is calculated by multipying the
‘umber of tes by the cost ofeach one.
‘© The grand totais created by adding $7 for shipping
Finally, the information is witten into the page by selecting elements
and then replacing the content ofthat element (sing technique you
‘meet flyin Chapter), lt selects elements rom the HTML page using
‘the vale oftheir 1d attributes and then updates the text inside those
clements
‘Once you have worked your way through his example, you should have
good basic understanding of how dat is stored vaables and how to
perform basic operations with the data those variables.
sasicsavascaprinstmucTionS (fiEXAMPLE
BASIC JAVASCRIPT INSTRUCTIONS
a2/ecnple. tent i
-<1D0CTYPE htmi>
Elderflower
" + docunent.URL + “
"s msg += ‘last modified: ' + docuaent.lastModitied + ‘: A gl i el innerHTHL = msgs $Seep228 for notes on using “nmeriML because con be asecunty rskiitis nt used conrecty The URL willook very diferent ityourun this page local rather than ona web server it il ely begin ith F¥1e:/// rather than swith etps/GLOBAL OBJECTS: STRING OBJECT Whenever you have a value that is a string, you can use the properties and methods of the String object on that value. This example stores the phrase "Home sweet home " in a variable. var saying = ‘Home sweet home '; These propertosandmethods -«sPROPERTY DESCRIPTION ate often used to work with ext eee isiex Returns number of charactersin the string In most cases (see note baton ett) ‘onthe right-hand page, cote Sonttcwoticneeetcnn) METHOD DESCRIPTION blest er” petit) Guten damon Succieeesissgag aaitaae() Conese SNitsrenatesorebes) ayy) Tani sano ics ranma. —— : Me nnn within your script, and wrapper actin Returns index number ofthe lst ime a character or object because it acts te 2 Tastindex0*() sot of characters found within the sting seeeeacnhser Ratu chris tue nde favre th hacer fet ax ‘object's properties and methods substring() umber is included and the character forthe last ‘onany value thats string index number is not includes ‘The Tength property counts When a character ie specified, spits the string themumberof"code uits"ina split) each time ts found, then stores each indvival part string: Inthe majority ofcases, —_amaray _ ee! ‘one character uses one code nit and most programmersuse —trin() Removes whitespace rom start and end of string ‘tik his. ut some of the rarely rae oe used characters take up two Like find and replace it takes one value that should rode units replace() _betfound, and another to replace it (by default it only replaces ithe frst matent tines) @ roncrions, moos onueersEach character in a string is automatically given a number, called an index number. Index numbers always start at zero and not one (just like for items in an array). Home sweet home @oe0e0000000 o eoe EXAMPLE esutt sient ieee i EXAMPLE - pesuit ying. tolpercue(); Wane avaet howe saying. toLovercese()s saying chart (12); saying. indexOf(‘ee"); saying-lastIndexOf(*e" saying substring (8,14) 5 Wome sweet home Home sweet sweet home di © sweet home Home sweet home ‘home sweet hone ' HOME SWEET HOME * saying.trin()s Home sweet home Home sweet hone "); Home sweet howe tone’, ‘sweet, ‘hone! *] "Home sweet home" runcrions,mernoos«onveers (3)WORKING WITH STRINGS This example demonstrates the 2. The next nels youhow The name ofthe variable Tenath poperty and many ofthe manycharacters are inthe sting (saying) followed by a dt, string object's methods shown _usingthe length property ofthe then the property or method that onthe previous page String abject and stoves the isbeing demonstrated Gn the resultina varisbe called msg, ‘same way thatthe other objects This example starts by storing 3. Theis followed by examples _inthis chapter used the dot the phrase Hone sweet hane ” showing severaof he String notion oindicatea property or inavariabe called saying. object's methods ‘method of an obec @ var saying = ‘Hone sweet hone *s @® var msg = '
length
' + saying. length +
" [insg += ‘uppercase
' + saying.toUppertase() +
|nsg += " ' + saying.toLowerCase() + ' ' + saying.charat(12) + " ' + saying. indexO#(ee!) + " " + saying. lastIndexOf("e") + *=/p>'s
fee's
Insg #= ‘ " + saying.substring(8, 14) + ‘ ' + saying.replace('me', ‘w') + ‘ Copyright Bcopys' + year + ‘You might also like