ServiceNow JavaScript Primer

Transcription

ServiceNowJavaScriptPrimer

:.14 trademarksoftheirrespectiveholders.2

1.Client- pagesandclientbrowsers2. Server- criptthatenablesback- ers(liketheaforementionedServiceNowGlideRecords)3. thclientandserver- riteclient- ‐sideandserver- ient- ‐sideandserver- thatyoucanusethatarespecifictoclient- ‐sideorserver- eashape.So,ifyouopenatexteditorandtype: scriptlanguage "JavaScript"type "text/javascript" !- ‐- ‐document.write(" fontcolor 'red' ThisIsRedText /font ")//- ‐- ‐ /script dTextdocument.write(" fontcolor 'red' ThisIsRedText /font redbywrite- isinsidetheparentheses(" fontcolor 'red' ThisIsRedText /font ")Remember,JavaScriptisanobject- iesaregroupedintologicalunitscalledobjects. trademarksoftheirrespectiveholders.3

yoftheWindowobjectlikethis:window.status need- ‐to- ‐know: EVERYTHINGiscase- ‐sensitivevarstringA "StringA";varstringa "Stringa";alert(stringA stringa);//false ringA "StringA";varstring "StringB"; cter. aslongastheclosingmatchestheopening. Parenthesesareusedfor2reasons:1. ue"or'false')if(stringA stringa){//dosomething}2. Sendingorreceivingavaluevarproduct urna*b;}//receive7as"a"&3as"b" ys:1. Single- safriendlyremindervarproduct multiply(4,6); trademarksoftheirrespectiveholders.4

JavaScriptPrimer2. Multi- ‐linecommentsaredenotedbyaslash- ‐startostart"/*"andastar- hefollowinglines//donotgetevaluatedvarproduct multiply(8,7);product twolinesofthesong(alongwitha" oes.varstringA 'Here\'stoyouMrs.Robinson\n' 'Jesuslovesyoumorethanyouwillknow';varstringB thiswhileinthrowwith trademarksoftheirrespectiveholders.5

varmyVariableName pesinJavaScript:1. Strings:2.3.4.5.varstring "Thisisastringofcharacters";Numbers:varnumber )varbool shavetomatchdatatypes,becareful)vararray named”propertiesvarobject {name:"Myfirstobject",type:"Object- rposes:1. Makereusablecode(modularization)2. Cleanyourcodeforlegibility trademarksoftheirrespectiveholders.6

gparts:1. e(a,b){varc cationfunction:functionmultiply(a,b){varc aswelike:varprod1 multiply(3,4);varprod2 multiply(prod1,17);varprod3 ut- ‐of- thereisavalidrecordinthefieldif(!newValue)return; trademarksoftheirrespectiveholders.7

JavaScriptPrimerg form.getReference('caller callerLabel gel('label.incident.caller id');varcallerField gel('sys display.incident.caller id');//checkforVIPstatusif(caller.vip groundImage groundRepeat 'no- ‐repeat';callerLabel.style.backgroundPosition allerField)callerField.style.color lerLabel)callerLabel.style.backgroundImage '';if(callerField)callerField.style.color nly1function:1. call)2. Oncetheserverresponds,checktheVIPflag3. IfthepersonisaVIP,flagthem4. utthelogic,andwhatthecodeshouldbedoing. trademarksoftheirrespectiveholders.8

){return;}g form.getReference('caller dsvarcallerLabel gel('label.incident.caller id');varcallerField gel('sys display.incident.caller id');//checkforVIPstatusif(caller.vip .backgroundImage Repeat 'no- ‐repeat';label.style.backgroundPosition field){field.style.color bel){label.style.backgroundImage '';}if(field){label.style.color lesfunctionrepeatedly. trademarksoftheirrespectiveholders.9

parisonOperatorsThekeytoaskingaquestion(if ourscripting: Equal! Notequal Greaterthan Greaterthanorequalto Lessthan Lessthanorequalto Equalto,andthesamedatatype(Identical)! andon.Let'stryit!if("a" "A"){alert('TheAshaveit!');//won'talert}if(0 "1"){alert('Naughtitis!');//willalert}if(0 happensifyouwanttocomparemultipleitemsatonetime? trademarksoftheirrespectiveholders.10

ifstatements:varprecipitation true;vartemperature 55;if(precipitation true){if(temperature D("&&")andOR(" ipitation true;vartemperature 55;if(precipitation true&&temperature lts:1. Bothcoldandwet2. Justcold3. Justwet4. Neither trademarksoftheirrespectiveholders.11

tation true;vartemperature 65;if(precipitation true&&temperature tation false&&temperature ation true&&temperature atement.Withtheelseifstatement,wecandevelopamulti- weneed:varprecipitation true;vartemperature 55;if(temperature 60&&precipitation ature 60&&precipitation itation norwet!"); trademarksoftheirrespectiveholders.12

s.ReviewSyntax Everythingiscase- geblocksofcodeVariables :1. String(stringofcharacters)2. Number3. Atrueorfalsevalue4. Array(asingleobjectthatholdsanumberofvalues)5. mbered)Functions hecodeIf/Elseif/Else checkmanyvaluesatatime,using&&(AND)and oormore"elseif"statements,andmayhaveoneelseclause trademarksoftheirrespectiveholders.13

JavaScriptPrimerExercisesExercise1–Variables Functions s,addsthemtogether,andmultipliesthatbyforty- gestothecodeaffecttheresult! ResourcesJavaScriptBooks: DavidFlanagan,O’ReillyNewtoJavaScript: FortheNon- er:Are- lla.org/en/JavaScript/A re- ‐introduction to JavaScriptOnlineResources: eveloper.mozilla.org/en/About ozilla.org/en/JavaScript/Guide/Details of the Object Model#Class- ‐Based vs. Prototype- ‐Based sions/html- ‐css- javascript.about.com/od/hintsandtips/Javascript w.learn- ‐javascript- ‐tutorial.com/ trademarksoftheirrespectiveholders.14

2014ServiceNow,Inc.Allrightsreserved. !! ServiceNowandtheServiceN