(PDF) JavaScript Programming
(PDF) JavaScript Programming
By Brian Draper
Cright 2017- All rights reserved
This dumnt i grd twrd providing exact nd rlibl infrmtin in rgrd to
th ti nd iu vrd. Th publication i sold with th id tht the publisher i
not rquird t render unting, officially rmittd, or thrwi, qulifid services.
If dvi i necessary, lgl r professional, a practiced individul in the profession
should be ordered.
Bfr starting u with th new language, u mut have a basic undrtnding of HTML
head and bd r, and fw iml ttmnt ud t dd img, inrt hyperlinks,
make rgrh in the hrtxt lngug. Th ftwr which u n use fr trting
th JvSrit tour i bi text ditr. Windows OS ur m go fr Notepad or
Notepad++ nd M Ur can u TxtEdit or TextWrangler. Th d would nd a
wb brwr (rfrbl Mozilla Firefox or Google Chrm) for successful
miltin and running.
Getting Strtd
<html>
<head>
<title>JavaScript example</title>
<rit lngug="JvSrit">
<!--
//-->
</rit>
</hd>
</html>
Th u of "<!-"and "//->" in the bv d ensures that th d is nt displayed b
older versions f brwr which lk JvSrit urt. This nur tht th g
d nt mlfuntin n unurtd brwr.
Jut lik n other HTML fil, you just need t copy t the above code in ur
Notepad dumnt, and thn save it with an extension of ".html". Aftr th v i
mlt, iml right lik n the file, and lt option "Open With". From hr,
h a web brwr (Mozilla Firfx if vilbl). Th d wuld xut on it
wn, and rult would b dild in the brwr windw.
Points to Rmmbr
JvSrit d are mostly placed in th "hd" rtin of the html but you m even
use thm under "bd" r.
A JavaScript will lw b mbddd int HTML code, and it can nvr tnd alone.
Alrt Message Bx
<body>
<script>
</script>
</body>
Similrl you m dd thr lrt like:
1. Confirm Action
Windw. Confirm ("Are you sure u wnt t exit from thi it?")
2. Prmt Action
User Intrtin
nblur
onchange
nlik
ondblclick
onfocus
nkdwn
nkr
nku
onload
onmousedown
nmumv
nmuut
nmuvr
nmuu
nlt
onsubmit
Rniv Wb Dign
These r some f th features tht give JvSrit n bilit t hndl iml wll
mlx tasks. Du t thi, JvSrit h rmind th mt popular rgrmming
language fr a long tim. It is l a good lngug for l wh wnt t learn
mutr rgrmming it urt bjt rintd as well funtin nt and t
use it, u jut need a brwr nd a text ditr.
How to Gt Rid of the JavaScript Wrning on
Intrnt Explorer
JvSrit, whih run lll, i urrntl ud hvil by wbit t drive their user
intrf. Althugh mn modern wbit mmunit with rvr t get thir dt,
tht communication n b lw r vn fil altogether. It is imrtnt fr th ur
intrf t be rniv even if there i a rblm with the server, the intrf is
usually writtn in JvSrit nd run lll frm ur computer, while th mr
complex d run n a server.
However, Internet Exlrr pops up a wrning dialog b default. Thi warning stops u
from ding nthing n th g nd n be nning. If u r a rgrmmr it m b
intrting t lk t th d, but for th rt f u it is nt vr uful.
T gt rid f the dilg, fllw th steps:
Thi huld t the warning dilg from showing. If u trt having rblm with
mn web g, thr may b a rblm with your brwr, nt with the web g, and
you may nd t rintll Intrnt Explorer.
Errr Abut JavaScript
Fr those not in th knw, the windw registry i a vast internal dtb in which ll
rgrm tht you u n your tm, including windw itlf, store a hug vlum f
dt. S whnvr u mk a hng t th Control Panel tting, fil itin,
system lii, r intll nw ftwr, th hng reflect in the rgitr. So the
rgitr tull mnitr the w ur tm bhv, nd unl u keep windows
rgitr ln b using Rgitr Clnr tool rgulrl, you will nt gt w from
JvSrit rrr mg.
E Tips You Cn U t Remove a JavaScript
Error
Your internet browser makes i f the places you go and stores thm in temporary
fil. A build up f these fil n rt rrr in th system nd th nd t be
mtid ridill. For Intrnt Explorer users do th following:
Mn Librri nd Frmwrk
You hv option t reduce th tim and effort required fr building lrg JavaScript
litin b uing several frmwrk nd libraries. Mn rgrmmr rfr using
dnmi JvSrit frameworks lik AngularJS, Bkbn, Ember and React. Hwvr,
you till hv tin to choose from a lng lit f JavaScript libraries rding t your
ifi needs. For intn, u n fftut GUI development uing widgt lik
DHTMLX, Bttr, jQur UI, DjWidgt or AngulrJS. Likewise, you u ulr
template tm like jQur Mobile, Hndlbr, Mustache nd Cd Frmwrk.
Cmrtivl Ftr
JvSrit further u the XmlHttRut API fr data rtrivl. The API nd HTTP
or HTTPS requests to th web rvr, nd load the rn nt b th rvr in th
script. Th rit n il udt the urrnt wb page bd n th rn dt
received from th rvr without rlding a nw web g. S u can boost the web
application's rfrmn b rviding updated infrmtin to the ur without
rlding the wb page mltl.
Extnd th Wbit' Funtinlit
Reduces Ld n Srvr
Apart frm all th issues, good t knw thir responsibilities nd have n excellent
professional utlk that mk all th diffrn. Th mjrit f th JvSrit
dvlmnt rgrm r particularly designed as well as developed for web-based
lng with diffrnt rvr-bd applications tht r gnrll used n web it and
mutr systems.
Aftr dvlmnt, it i nr t evaluate th programs properly. A gd dvlr
wuld also have the xrti t vlut th rgrmming scripts nd hn,
mni hir them to ut t a rgrmmr will b able t develop wll
vlut th scripts. They are also xrt in testing the lngug xtnivl t see
whthr it i funtining properly bfr it i hndd vr t th lint. Th also nur
that th script i ttll fr of bug.
Th <script> Tag
Example
<rit>
document.getElementById("demo").innerHTML = "My Firt JavaScript";
</rit>
JavaScript in <hd>
Exml
<!DOCTYPE html>
<html>
<hd>
<rit>
funtin myFunction() {
dumnt.gtElmntBId("dm").innrHTML = "Paragraph hngd.";
}
</script>
</hd>
<bd>
<h1>A Wb Pg</h1>
<p id="dm">A Paragraph</p>
<buttn type="button" nlik="mFuntin()">Tr it</button>
</bd>
</html>
JvSrit in <body>
In thi xml, a JvSrit funtin i placed in the <bd> section of n HTML page.
Example
<!DOCTYPE html>
<html>
<body>
</bd>
</html>
Extrnl JvSrit
Example
<!DOCTYPE html>
<html>
<body>
<script r="mSrit.j"></rit>
</bd>
</html>
It rt HTML nd d
It mk HTML nd JvSrit ir t read nd mintin
Chd JavaScript fil can d u page ld
T add vrl script fil t n page - use vrl rit tg:
Example
<script r="mSrit1.j"></rit>
<script src="myScript2.js"></script>
Extrnl Rfrn
External rit n be rfrnd with a full URL or with a th relative t the urrnt
wb page.
Exml
<rit r="htt://www.w3hl.m/j/mSrit1.j"></rit>
Exml
<script src="myScript1.js"></script>
JvSrit Output
Exml
<!DOCTYPE html>
<html>
<body>
<rit>
windw.lrt(5 + 6);
</script>
</bd>
</html>
Using dumnt.writ()
For tting ur, it i convenient to u document.write():
Exml
<!DOCTYPE html>
<html>
<bd>
<script>
dumnt.writ(5 + 6);
</rit>
</bd>
</html>
Uing document.write() ftr n HTML dumnt is fully ldd, will delete ll xiting
HTML:
Exml
<!DOCTYPE html>
<html>
<bd>
</body>
</html>
Uing innrHTML
T n HTML lmnt, JavaScript n u the dumnt.gtElmntBId(id)
method.
Example
<!DOCTYPE html>
<html>
<body>
<p id="dm"></>
<rit>
dumnt.gtElmntBId("dm").innrHTML = 5 + 6;
</script>
</bd>
</html>
Uing console.log()
Example
<!DOCTYPE html>
<html>
<bd>
<rit>
nl.lg(5 + 6);
</script>
</bd>
</html>
JvSrit Syntax
JvSrit Programs
A mutr program is a lit f "intrutin" t b "executed" b the computer.
In a programming language, th rgrm intrutin r lld statements.
Exml
vr x, , z;
x = 5;
y = 6;
z = x + ;
JvSrit Statements
JvSrit ttmnt are md f:
JvSrit Vlu
The JavaScript ntx defines tw t of vlu: Fixd vlu and vribl values.
Fixed vlu are called litrl. Variable values are lld variables.
JavaScript Literals
Th mt imrtnt rul fr writing fixed vlu r:
10.50
1001
"Jhn D"
'John Doe'
JvSrit Vribl
In a rgrmming lngug, vribl r ud t tr dt vlu.
var x;
x = 6;
JavaScript Operators
var x, ;
x = 5;
y = 6;
JvSrit u rithmti operators ( + - * / ) to mut vlu:
JvSrit Expressions
5 * 10
x * 10
vr x, y;
x = 5 + 6;
y = x * 10;
JavaScript Cmmnt
Not ll JvSrit statements r "executed".
vr x = 5; // I will be executed
JvSrit Identifiers
Idntifir r names.
vr lastname, ltNm;
lastName = "D";
ltnm = "Ptrn";
JvSrit nd Camel C
Hhn:
Underscore:
mlC
JvSrit Character St
This ttmnt tll the browser t writ "Hello Dll." inid an HTML element with
id="demo":
Exml
JvSrit Prgrm
Most JavaScript programs ntin many JvSrit ttmnt.
Th ttmnt are xutd, one b one, in the same rdr they r written.
Exml
vr x, , z;
x = 5;
y = 6;
z = x + ;
document.getElementById("demo").innerHTML = z;
Smiln ;
vr , b, ;
a = 5;
b = 6;
c = a + b;
a = 5; b = 6; c = a + b;
JavaScript White S
vr rn = "Hege";
var rn="Hg";
vr x = y + z;
JvSrit Line Length nd Lin Brk
If a JvSrit ttmnt d nt fit n one line, the best l t break it, i after an
operator:
Exml
document.getElementById("demo").innerHTML =
"Hll Dll.";
JvSrit Code Blk
JavaScript ttmnt n be grouped tgthr in code blk, inid url brkt
{...}.
Example
funtin mFuntin() {
document.getElementById("demo1").innerHTML = "Hello Dolly.";
dumnt.gtElmntBId("dm2").innrHTML = "How are u?";
}
JavaScript Keywords
Hr i a lit f some f the kwrd you will lrn but in thi tutorial:
Kwrd Dritin
brk - Trmint a with or a l
do ... while Exut a blk f ttmnt, and repeats th blk, whil a condition
is true
Any text between // and th end of the line will b ignrd b JavaScript (will not b
executed).
Exml
// Chng hding:
dumnt.gtElmntBId("mH").innrHTML = "M First Pg";
// Chng paragraph:
document.getElementById("myP").innerHTML = "M firt rgrh.";
Thi xml uses a ingl line mmnt t th end f h lin t explain th code:
Exml
var x = 5; // Declare x, giv it th value of 5
var y = x + 2; // Dlr , give it th vlu of x + 2
Multi-line Cmmnt
Multi-lin mmnt trt with /* nd nd with */.
Any txt btwn /* nd */ will b ignored b JvSrit.
Exml
/*
Th d blw will hng
th heading with id = "myH"
and th rgrh with id = "mP"
in m wb g:
*/
dumnt.gtElmntBId("mH").innrHTML = "M Firt Page";
dumnt.gtElmntBId("mP").innrHTML = "My first rgrh.";
Exml
//dumnt.gtElmntBId("mH").innrHTML = "M Firt Pg";
dumnt.gtElmntBId("mP").innrHTML = "M firt rgrh.";
Exml
/*
document.getElementById("myH").innerHTML = "M Firt Pg";
dumnt.gtElmntBId("mP").innrHTML = "M first paragraph.";
*/
JvSrit Vribl
JvSrit Variables
JavaScript vribl r ntinr fr tring dt values.
Exml
var x = 5;
vr y = 6;
vr z = x + y;
* x tr the value 5
* y tr th vlu 6
* z tr th vlu 11
Exml
var ri1 = 5;
vr price2 = 6;
var total = ri1 + ri2;
In rgrmming, jut like in lgbr, we use variables (lik price1) to hld values.
JavaScript Idntifir
Idntifir can b hrt names (lik x and ) r mr dritiv names (age, um,
totalVolume).
Th Aignmnt Operator
In JvSrit, th equal ign (=) i n "ignmnt" operator, not n "ul to" operator.
Thi i diffrnt from algebra. The fllwing does not mk sense in algebra:
x=x+5
In JvSrit, hwvr, it mk rft n: it ign th vlu of x + 5 to x.
JavaScript can handle many types f data, but fr nw, jut think of numbers and tring.
Strings r writtn inside dubl r ingl ut. Numbers r written withut quotes.
Exml
vr i = 3.14;
vr rn = "John Doe";
vr nwr = 'Y I m!';
var carName;
After the dlrtin, th variable has n value. (Thnill it h th vlu f
undefined)
rNm = "Vlv";
Exml
< id="dm"></>
<rit>
var rNm = "Volvo";
dumnt.gtElmntBId("dm").innrHTML = carName;
</rit>
On Statement, Mn Variables
You n dlr mn vribl in one ttmnt.
Vlu = undfind
Th vribl rNm will have the value undefined after th xutin f this
statement:
Example
vr rNm;
The vribl rNm will till have the value "Vlv" after th xutin f these
statements:
Example
vr rNm = "Vlv";
vr rNm;
JvSrit Arithmetic
As with algebra, you can d rithmti with JvSrit vribl, using operators lik =
nd +:
Example
var x = 5 + 2 + 3;
Example
var x = "Jhn" + " " + "Doe";
Al tr this:
Exml
vr x = "5" + 2 + 3;
JavaScript Ortr
Example
Aign vlu t vribl nd dd thm tgthr:
Aignmnt
var x = 10;
Adding
vr x = 5;
vr y = 2;
vr z = x + ;
Multiling
var x = 5;
vr y = 2;
var z = x * ;
Ortr Dritin
+ Additin
- Subtrtin
* Multilitin
/ Diviin
% Modulus
++ Inrmnt
-- Drmnt
Assignment
var x = 10;
x += 5;
Aignmnt operators r full described in th JS Aignmnt htr.
Exml
Exml
txt1 = "What a vr ";
txt1 += "ni d";
Th rult f txt1 will b:
Wht a vr ni d
10
55
Hll5
alert(Answer);
Basic Programming Skill in thi Bit f Code
3. Th firt lin f thi script d thr jobs, 1) it tells JvSrit to set id memory
fr th vribl SquareThis, 2) it u a r- writtn funtin t rmt th ur
for a numbr, 3) it takes whtvr the ur types in th prompt box and ut it in th
mmr fr SurThi.
4. Notice tht th funtin rmt requires n rgumnt just lik th lrt() did. Nti
too that the argument is in " ", which mn tht the rgumnt i a String data t.
5. Nw the script nd a variable to hold the answer. This i dn with "vr Anwr;"
6. Evrthing is in l nw t d th mth: "Anwr = SurThi * SurThi;" Th
rt that tells the mutr wht kind f mth to d is lld n rtr. Th min
rtr r +, -, *, / (add, ubtrt, multil, divid). There are mn thr rtr,
wll. A ur is any numbr multiplied by itself; thi frmul multili
SquareThis b SurThi.
Ortr
+ Adds tw numbr
- Subtrt tw numbr
/ Divid tw numbers
Brian Draper