Java Script
Java Script
11/
conversions
executes
Evaluates a string and
eval) a numeric value
to
Converts a string expression
parselt) varx 10;
parseFloat() var y 20;
y")+ "<br>";
*
var a =
eval("x
"<br>":
var b eval("2 +2")+
=
"<br>";
varc eval("x + 17")+
Output
200
27
parselnt() parseFloat()
Parsesa string argument and returns an integer
F n returns the first
integer contained in string or 0 if the and returns a floating point
string does not begin with an integer. Parses a string argument
number.
var a parseint("10")+ "<br>";
Only the first number in the string is returned!
var c= parselnt("10.33")+ "<br»"; Leading and trailing spaces are allowed.
var d= parseint"34 45 66")+"<br>"; number,
var e parselnt(" 60 ") + "<br>";
the first character cannot be converted to a
IfparseFloat() returns NaN.
var f parseint("40 years") + "<br"; var c= parseFloat("1o.33")
HTML poge within the <HEAD. HEAD» ug thea the JavaScrigi DOM picks up whoie at nw
- Docunent of objects, which add to the stardard DOM objects already recognized by lavaacript The addiboal
- Anchor objects brought into the DOM by JSSS are shown in diagran
-Lik 1- Docunent
I-> For
Tags
I-> textbox
- > DIV
Itextarea
-> radiabutton - SPAN
- checkbox ->M1
->elasses
through N6
-> Tag Names
select 1-IDS
-button
How a JavaScript handled browse
Browser Objects handles the Document object
when any JvaScript enabled browser loadsa web page, the brower automatically creates a number of
JavaScript odjects that map to the DOM (or JSS DOAM. IN is the DOM, whkh provides Jsvascipt access Any document (ie. the HTML poge) can contain varios HTML objects wuch as:
to the HTML ojats that Images
aT contained n the web ***************** ******************************** a Image Maps
The Mavigator objecE The Web Paq
A
JATASscrip
Browser The windo object oded in the
a Hyperiinks
The JavaScript code ased windev objece J browser Frams
snippets imbedded s MAppingto the The Docunent objeet nao
containlng all Anchors
of he web mge the KTMu
5 Locatioa dbject lenents to Applets
el (e
e serea which h e Mulimedia objects such a audio files, streaning video files
hin theSCRIPT oojeetThe Riatorr
objectJavascript code O A Fom with various form elements
<SCRPT> a of T MaT Oject Tae veat objectrequires accesa. The browser creates one aAY in memory oEr HITML odisst in
filenae htmi) makes ue
the Coon Context Area**************************************
in coeputer nenory (RAN) HTML objects. the documen thus registering each of these
teract t h the HTML If these HTML
obyecs in the wed p a objects are actually contained in the HTML
slements, which will point to the contel area in page then these aTays will boid indsxsd
in in. where the HTML object are Otherwise the a y
will exist, but will be empty (i.e. have no elementsmemory
ot 2.
a tuat slícle f
A cookie is an amount ofinformation that persists between a When a user sends to the server, then each
a request of that
server-side and a client-side. request is treated asa new request sent by the different user.
A web browser stores this information at the time of S o , to recognize the old user, we need to add the cookie with
browsing. the response from the server.
A cookie contains the information as a string generally in the browser at the client-side.
form of a name-value palir separated by semi-colons. Now, whenever a user sends a request to the serve, the
I t maintains the state of a user and remembers the user's cookle is added with that request automatically. Due to the
information among all the web pages. cookle, the server recognizes the users.
How Cookies Works? How to create a Cookie in JavaScript?
In JavaScript, we can create, read,
update and delete a cookie
by using document.cookie
property.
eguest The following syntax is used to create
Kepone*Coolie
a
cookie
RequesteCookie
document.cookie="name=value";
Web Browiw
Server
document.cookie "color="+value;
window.onload = function ()
if (document.cookie.length I= 0) {
var array = document.cookie.split("=");
.The window object allows execution of code at specified T h e window.setTimeout() method can be written without
The two key methods to use with JavaScript are: The first parameter is a function to be executed.
milliseconds)
Executes a function, after waiting a specified number of before execution.
<button onclick="setTimeout(myFunction, 3000)">Try it
milliseconds.
</button>
setintervallfunction, milliseconds) Click a button.
Wait3
Same as set Timeout(), but repeats the execution of the <script> seconds, and the page wil
function myFunction() { alert "Hello":
function continuously. alert('Hello');
</script>
Both methodsare of the HTML DOM Windowobject._
Display Stoptime
b u t tid-"demo"></h4>
ch4 on onclick="clearinterval(myVar)">Stop time</button>
Method
window.clearinterval(timerVoriable)
can
be written <script>
without the window prefix. setinterval(myTimer,
100o);
Uses the variable myVar
varfunction myTimer() {
returned from setinterval():
var d = new Date(0
myVar =
d.toLocale TimeString():
T M L
clearlntenvamyvar)
</script </body </html>
This attribute takes an exact date (in GMT/UTC format) when optional max-age attribute
the cookie should expire, rather than an offset in seconds.
1 1 Set for 30 days
document.cookie = "firstName=Christopher
Lab Lab
Q Using setTimeout() and clearTimeout() do the following
Q3.
Write JavaScript to show the usage of setinterval function using
Use button to show alert "Hello" after 3s.
which background color of a division keeps on changing to a
Use "Stop" button to prevent the first function to execute. random color every 2 seconds.
Also use setTimeout function to show the content of a textbox
Q Using setimterval() and clearintervai() as label of large size after 3 seconds.
display the current time, and stop the timer. 04.
Prompt user to enter his name and add it to cookies. If the user
is new user then show an alert box stating Welcome <user
name> otherwise display Welcome again <user name>.
Cookies should get removed already after 24 hours