Page 1
What is JavaScript?
__rendered_path__1
A1: JavaScript is a general-purpose programming language designed to let
__rendered_path__4
programmers of all skill levels control the behavior of software objects. The language is
__rendered_path__4
used most widely today in Web browsers whose software objects tend to represent a
__rendered_path__9
variety of HTML elements in a document and the document itself. But the language can
__rendered_path__4
be--and is--used with other kinds of objects in other environments. For example, Adobe
__rendered_path__4
Acrobat Forms uses JavaScript as its underlying scripting language to glue together
__rendered_path__9
objects that are unique to the forms generated by Adobe Acrobat. Therefore, it is
__rendered_path__4
important to distinguish JavaScript, the language, from the objects it can communicate
__rendered_path__4
with in any particular environment. When used for Web documents, the scripts go
__rendered_path__4
directly inside the HTML documents and are downloaded to the browser with the rest of
__rendered_path__9
the HTML tags and content.
__rendered_path__4
__rendered_path__4
A2:JavaScript is a platform-independent,event-driven, interpreted client-side scripting
__rendered_path__9
and programming language developed by Netscape Communications Corp. and Sun
__rendered_path__4
Microsystems.
__rendered_path__38
How is JavaScript different from Java?
__rendered_path__41
JavaScript was developed by Brendan Eich of Netscape; Java was developed at Sun
__rendered_path__4
Microsystems. While the two languages share some common syntax, they were
__rendered_path__9
developed independently of each other and for different audiences. Java is a full-fledged
__rendered_path__4
programming language tailored for network computing; it includes hundreds of its own
__rendered_path__4
objects, including objects for creating user interfaces that appear in Java applets (in
__rendered_path__9
Web browsers) or standalone Java applications. In contrast, JavaScript relies on
__rendered_path__4
whatever environment it's operating in for the user interface, such as a Web
__rendered_path__4
document's form elements.
__rendered_path__9
JavaScript was initially called LiveScript at Netscape while it was under development. A
__rendered_path__4
licensing deal between Netscape and Sun at the last minute let Netscape plug the
__rendered_path__4
"Java" name into the name of its scripting language. Programmers use entirely different
__rendered_path__4
tools for Java and JavaScript. It is also not uncommon for a programmer of one
__rendered_path__9
language to be ignorant of the other. The two languages don't rely on each other and
__rendered_path__4
are intended for different purposes. In some ways, the "Java" name on JavaScript has
__rendered_path__4
confused the world's understanding of the differences between the two. On the other
__rendered_path__9
hand, JavaScript is much easier to learn than Java and can offer a gentle introduction
__rendered_path__4
for newcomers who want to graduate to Java and the kinds of applications you can
__rendered_path__4
develop with it.
__rendered_path__73
What’s relationship between JavaScript and ECMAScript?
__rendered_path__9
ECMAScript is yet another name for JavaScript (other names include LiveScript). The
__rendered_path__9
current JavaScript that you see supported in browsers is ECMAScript revision 3.
__rendered_path__81
How do you submit a form using Javascript?
__rendered_path__41
Use document.forms[0].submit();
__rendered_path__4
(0 refers to the index of the form if you have more than one form in a page, then the
__rendered_path__9
first one has the index 0, second has index 1 and so on).
__rendered_path__81
How do we get JavaScript onto a web page?
__rendered_path__41
You can use several different methods of placing javascript in you pages.
__rendered_path__4
You can directly add a script element inside the body of page.
__rendered_path__9
1. For example, to add the "last updated line" to your pages, In your page text, add the
__rendered_path__4
following:
__rendered_path__4
<p>blah, blah, blah, blah, blah.</ p>
__rendered_path__4
<script type="text/ javascript" >
__rendered_path__9
<!
--
Hiding from old browsers
__rendered_path__4
document.write("Last Updated:" +
__rendered_path__4__rendered_path__9
document.lastModified);

Page 2
document.close();
__rendered_path__1
/ / -->
__rendered_path__4
</ script>
__rendered_path__1
<p>yada, yada, yada.</ p>
__rendered_path__11
(Note: the first comment, "<--" hides the content of the script from browsers that don't
__rendered_path__4
understand javascript. The "/ / -->" finishes the comment. The "/ / " tells javascript that
__rendered_path__1
this is a comment so javascript doesn't try to interpret the "-->". If your audience has
__rendered_path__1
much older browsers, you should put this comments inside your javascript. If most of
__rendered_path__4
your audience has newer browsers, the comments can be omitted. For brevity, in most
__rendered_path__1
examples here the comments are not shown. )
__rendered_path__1
The above code will look like this on Javascript enabled browsers,
__rendered_path__4
2. Javascript can be placed inside the <head> element
__rendered_path__1
Functions and global variables typically reside inside the <head> element.
__rendered_path__1
<head>
__rendered_path__1
<title>Default Test Page</ title>
__rendered_path__4
<script language="JavaScript" type="text/ javascript">
__rendered_path__1
var myVar = "";
__rendered_path__1
function timer(){setTimeout('restart()',10);}
__rendered_path__4
document.onload=timer();
__rendered_path__1
</ script>
__rendered_path__1
</ head>
__rendered_path__1
__rendered_path__4
Javascript can be referenced from a separate file
__rendered_path__1
Javascript may also a placed in a separate file on the server and referenced from an
__rendered_path__1
HTML page. (Don't use the shorthand ending "<script ... / >). These are typically placed
__rendered_path__4
in the <head> element.
__rendered_path__1
<script type="text/ javascript" SRC="myStuff.js"></ script>
__rendered_path__11
How to read and write a file using javascript?
__rendered_path__69
I/ O operations like reading or writing a file is not possible with client-side javascript.
__rendered_path__4
However , this can be done by coding a Java applet that reads files for the script.
__rendered_path__76
How to detect the operating system on the client machine?
__rendered_path__69
In order to detect the operating system on the client machine, the
__rendered_path__1
navigator.appVersion
__rendered_path__1
string (property) should be used.
__rendered_path__87
How can JavaScript make a Web site easier to use? That is, are there certain
__rendered_path__69
JavaScript techniques that make it easier for people to use a Web site?
__rendered_path__69
JavaScript's greatest potential gift to a Web site is that scripts can make the page more
__rendered_path__1
immediately interactive, that is, interactive without having to submit every little thing
__rendered_path__1
to the server for a server program to re-render the page and send it back to the client.
__rendered_path__1
For example, consider a top-level navigation panel that has, say, six primary image
__rendered_path__4
map links into subsections of the Web site. With only a little bit of scripting, each map
__rendered_path__1
area can be instructed to pop up a more detailed list of links to the contents within a
__rendered_path__1
subsection whenever the user rolls the cursor atop a map area. With the help of that
__rendered_path__4
popup list of links, the user with a scriptable browser can bypass one intermediate
__rendered_path__1
menu page. The user without a scriptable browser (or who has disabled JavaScript) will
__rendered_path__1
have to drill down through a more traditional and time-consuming path to the desired
__rendered_path__1
content.
__rendered_path__87
What are JavaScript types?
__rendered_path__69__rendered_path__1
Number, String, Boolean, Function, Object, Null, Undefined.

Page 3
How do you convert numbers between different bases in JavaScript?
__rendered_path__1
Use the parseInt() function, that takes a string as the first parameter, and the base as a
__rendered_path__6
second parameter. So to convert hexadecimal 3F to decimal, use parseInt ("3F", 16);
__rendered_path__8
How to create arrays in JavaScript?
__rendered_path__11
We can declare an array like this
__rendered_path__11
var scripts = new Array();
__rendered_path__6
We can add elements to this array like this
__rendered_path__6
__rendered_path__11
scripts[0] = "PHP";
__rendered_path__6
scripts[1] = "ASP";
__rendered_path__6
scripts[2] = "JavaScript";
__rendered_path__6
scripts[3] = "HTML";
__rendered_path__11
__rendered_path__6
Now our array scrips has 4 elements inside it and we can print or access them by using
__rendered_path__6
their index number. Note that index number starts from 0. To get the third element of
__rendered_path__11
the array we have to use the index number 2 . Here is the way to get the third element
__rendered_path__6
of an array.
__rendered_path__6
document.write(scripts[2]);
__rendered_path__6
We also can create an array like this
__rendered_path__11
var no_array = new Array(21, 22, 23, 24, 25);
__rendered_path__49
How do you javascript_viva questions a specific frame from a hyperlink?
__rendered_path__1
Include the name of the frame in the javascript_viva questions attribute of the hyperlink. <a
__rendered_path__6
href=”mypage.htm” javascript_viva questions=”myframe”>>My Page</ a>
__rendered_path__8
What is a fixed-width table and its advantages?
__rendered_path__8
Fixed width tables are rendered by the browser based on the widths of the columns in
__rendered_path__6
the first row, resulting in a faster display in case of large tables. Use the CSS style
__rendered_path__11
table-layout:fixed to specify a fixed width table.
__rendered_path__6
If the table is not specified to be of fixed width, the browser has to wait till all data is
__rendered_path__6
downloaded and then infer the best width for each of the columns. This process can be
__rendered_path__6
very slow for large tables.
__rendered_path__8
Example of using Regular Expressions for syntax checking in JavaScript
__rendered_path__6
__rendered_path__11
__rendered_path__6
...
__rendered_path__6
var re = new RegExp("^(&[A-Za-z_0-9]{1,}=[A-Za-z_0-9]{1,})*$");
__rendered_path__11
var text = myWidget.value;
__rendered_path__6
var OK = re.test(text);
__rendered_path__6
if( ! OK ) {
__rendered_path__6
alert("The extra parameters need some work.
\
r
\
n Should be something like:
__rendered_path__11
\"&a=1&c=4\"");
__rendered_path__6
}
__rendered_path__114
Where are cookies actually stored on the hard disk?
__rendered_path__1
This depends on the user's browser and OS.
__rendered_path__6
In the case of Netscape with Windows OS,all the cookies are stored in a single file called
__rendered_path__6
__rendered_path__11
cookies.txt
__rendered_path__6
c:\Program Files\Netscape\Users\username\cookies.txt
__rendered_path__6
In the case of IE,each cookie is stored in a separate file namely username@website.txt.
__rendered_path__6__rendered_path__145
c:\Windows\Cookies\username@Website.txt

Page 4
How to add Buttons in JavaScript?
__rendered_path__1
The most basic and ancient use of buttons are the "submit" and "clear", which appear
__rendered_path__4
slightly before the Pleistocene period. Notice when the "GO!" button is pressed it
__rendered_path__4
submits itself to itself and appends the name in the URL.
__rendered_path__8
<form action="" name="buttonsGalore" method="get">
__rendered_path__4
Your Name: <input type="text" name="mytext" / >
__rendered_path__4
<br / >
__rendered_path__4
<input type="submit" value="GO!" / >
__rendered_path__8
<input type="reset" value="Clear All" / >
__rendered_path__4
</ form>
__rendered_path__4
__rendered_path__8
Another useful approach is to set the "type" to "button" and use the "onclick" event.
__rendered_path__4
<script type="text/ javascript">
__rendered_path__4
function displayHero(button) {
__rendered_path__4
alert("Your hero is \""+button.value+"\".");
__rendered_path__8
}
__rendered_path__39
</ script>
__rendered_path__8
__rendered_path__4
<form action="" name="buttonsGalore" method="get">
__rendered_path__4
<fieldset style="margin: 1em; text-align: center;">
__rendered_path__47
<legend>Select a Hero</ legend>
__rendered_path__4
<input type="button" value="Agamemnon" onclick="displayHero(this)" / >
__rendered_path__8
<input type="button" value="Achilles" onclick="displayHero(this)" / >
__rendered_path__4
<input type="button" value="Hector" onclick="displayHero(this)" / >
__rendered_path__4
<div style="height: 1em;" / >
__rendered_path__4
</ fieldset>
__rendered_path__8
</ form>
__rendered_path__66
What can javascript programs do?
__rendered_path__1
Generation of HTML pages on-the-fly without accessing the Web server. The user can
__rendered_path__4
be given control over the browser like User input validation Simple computations can
__rendered_path__8
be performed on the client's machine The user's browser, OS, screen size, etc. can be
__rendered_path__4
detected Date and Time Handling
__rendered_path__80
How to set a HTML document's background color?
__rendered_path__8
document.bgcolor property can be set to any appropriate color.
__rendered_path__80
How can JavaScript be used to personalize or tailor a Web site to fit individual
__rendered_path__1
users?
__rendered_path__8
JavaScript allows a Web page to perform "if-then" kinds of decisions based on browser
__rendered_path__8
version, operating system, user input, and, in more recent browsers, details about the
__rendered_path__4
screen size in which the browser is running. While a server CGI program can make
__rendered_path__4
some of those same kinds of decisions, not everyone has access to or the expertise to
__rendered_path__4
create CGI programs. For example, an experienced CGI programmer can examine
__rendered_path__8
information about the browser whenever a request for a page is made; thus a server so
__rendered_path__4
equipped might serve up one page for Navigator users and a different page for Internet
__rendered_path__4
Explorer users. Beyond browser and operating system version, a CGI program can't
__rendered_path__8
know more about the environment. But a JavaScript-enhanced page can instruct the
__rendered_path__4
browser to render only certain content based on the browser, operating system, and
__rendered_path__4
even the screen size.
__rendered_path__4
Scripting can even go further if the page author desires. For example, the author may
__rendered_path__8
include a preference screen that lets the user determine the desired background and
__rendered_path__4
text color combination. A script can save this information on the client in a well-
__rendered_path__4
regulated local file called a cookie. The next time the user comes to the site, scripts in
__rendered_path__8__rendered_path__4
its pages look to the cookie info and render the page in the color combination selected

Page 5
previously. The server is none the wiser, nor does it have to store any visitor-specific
__rendered_path__1
information.
__rendered_path__7
Are you concerned that older browsers don't support JavaScript and thus
__rendered_path__10
exclude a set of Web users? individual users?
__rendered_path__12
Fragmentation of the installed base of browsers will only get worse. By definition, it can
__rendered_path__12
never improve unless absolutely everyone on the planet threw away their old browsers
__rendered_path__1
and upgraded to the latest gee-whiz versions. But even then, there are plenty of
__rendered_path__1
discrepancies between the scriptability of the latest Netscape Navigator and Microsoft
__rendered_path__12
Internet Explorer.
__rendered_path__1
The situation makes scripting a challenge, especially for newcomers who may not be
__rendered_path__1
aware of the limitations of earlier browsers. A lot of effort in my books and ancillary
__rendered_path__1
material goes toward helping scripters know what features work in which browsers and
__rendered_path__12
how to either workaround limitations in earlier browsers or raise the compatibility
__rendered_path__1
common denominator.
__rendered_path__1
Designing scripts for a Web site requires making some hard decisions about if, when,
__rendered_path__12
and how to implement the advantages scripting offers a page to your audience. For
__rendered_path__1
public Web sites, I recommend using scripting in an additive way: let sufficient content
__rendered_path__1
stand on its own, but let scriptable browser users receive an enhanced experience,
__rendered_path__1
preferably with the same HTML document.
__rendered_path__40
What does isNaN function do?
__rendered_path__10
Return true if the argument is not a number.
__rendered_path__7
What is negative infinity?
__rendered_path__49
It’s a number in JavaScript, derived by dividing negative number by zero.
__rendered_path__40
In a pop-up browser window, how do you refer to the main browser window
__rendered_path__12
that opened it?
__rendered_path__10
Use window.opener to refer to the main window from pop-ups.
__rendered_path__40
How to write a script for "Select" lists using javascript
__rendered_path__12
1. To remove an item from a list set it to null
__rendered_path__12
mySelectObject.options[3] = null;
__rendered_path__1
2. To truncate a list set its length to the maximum size you desire
__rendered_path__1
mySelectObject.length = 2;
__rendered_path__12
3. To delete all options in a select object set the length to 0.
__rendered_path__1
mySelectObject.leng
__rendered_path__40
Text From Your Clipboard?
__rendered_path__12
It is true, text you last copied for pasting (copy & paste) can be stolen when you visit
__rendered_path__12
web sites using a combination of JavaScript and ASP (or PHP, or CGI) to write your
__rendered_path__1
possible sensitive data to a database on another server.
__rendered_path__7
What does the "Access is Denied" IE error mean?
__rendered_path__10
The "Access Denied" error in any browser is due to the following reason.
__rendered_path__1
A javascript in one window or frame is tries to access another window or frame whose
__rendered_path__12
document's domain is different from the document containing the script.
__rendered_path__7
Is a javascript script faster than an ASP script?
__rendered_path__10
Yes.Since javascript is a client-side script it does require the web server's help for its
__rendered_path__1
computation,so it is always faster than any server-side script like ASP,PHP,etc..
__rendered_path__40
Are Java and JavaScript the Same?
__rendered_path__10__rendered_path__1
No.java and javascript are two different languages.

Page 6
Java is a powerful object
-
oriented programming language l
ike C++,C whereas
__rendered_path__1
Javascript is a client-side scripting language with some limitations.
__rendered_path__7
How to embed javascript in a web page?
__rendered_path__12
javascript code can be embedded in a web page between <script
__rendered_path__1
langugage="javascript"></ script> tags
__rendered_path__17
What and where are the best JavaScript resources on the Web?
__rendered_path__20
The Web has several FAQ areas on JavaScript. The best place to start is something
__rendered_path__20
called the meta-FAQ [14-Jan-2001 Editor's Note: I can't point to it anymore, it is
__rendered_path__1
broken!], which provides a high-level overview of the JavaScript help available on the
__rendered_path__1
Net. As for fact-filled FAQs, I recommend one maintained by Martin Webb and a mini-
__rendered_path__1
FAQ that I maintain.
__rendered_path__20
For interactive help with specific problems, nothing beats the primary JavaScript
__rendered_path__1
Usenet newsgroup, comp.lang.javascript. Depending on my work backlog, I answer
__rendered_path__1
questions posted there from time to time. Netscape and Microsoft also have vendor-
__rendered_path__20
specific developer discussion groups as well as detailed documentation for the scripting
__rendered_path__1
and object model implementations.
__rendered_path__17
What are the problems associated with using JavaScript, and are there
__rendered_path__20
JavaScript techniques that you discourage?
__rendered_path__12
Browser version incompatibility is the biggest problem. It requires knowing how each
__rendered_path__1
scriptable browser version implements its object model. You see, the incompatibility
__rendered_path__20
rarely has to do with the core JavaScript language (although there have been
__rendered_path__1
improvements to the language over time); the bulk of incompatibility issues have to do
__rendered_path__1
with the object models that each browser version implements. For example, scripters
__rendered_path__1
who started out with Navigator 3 implemented the image rollover because it looked
__rendered_path__20
cool. But they were dismayed to find out that the image object wasn't scriptable in
__rendered_path__1
Internet Explorer 3 or Navigator 2. While there are easy workarounds to make this
__rendered_path__1
feature work on newer browsers without disturbing older ones, it was a painful learning
__rendered_path__20
experience for many.
__rendered_path__1
The second biggest can of worms is scripting connections between multiple windows. A
__rendered_path__1
lot of scripters like to have little windows pop up with navigation bars or some such
__rendered_path__1
gizmos. But the object models, especially in the older browser versions, don't make it
__rendered_path__20
easy to work with these windows the minute you put a user in front of them--users who
__rendered_path__1
can manually close windows or change their stacking order. More recently, a glitch in
__rendered_path__1
some uninstall routines for Windows 95 applications can disturb vital parts of the
__rendered_path__20
system Registry that Internet Explorer 4 requires for managing multiple windows. A
__rendered_path__1
scripter can't work around this problem, because it's not possible to detect the problem
__rendered_path__1
in a user's machine. I tend to avoid multiple windows that interact with each other. I
__rendered_path__1
think a lot of inexperienced Web surfers can also get confused by them.
__rendered_path__17
What Boolean operators does JavaScript support?
__rendered_path__12
&&, || and !
__rendered_path__7
What does "1"+2+4 evaluate to?
__rendered_path__12
Since 1 is a string, everything is a string, so the result is 124.
__rendered_path__99
How to get the contents of an input box using Javascript?
__rendered_path__12
Use the "value" property.
__rendered_path__1
var myValue = window.document.getElementById("MyTextBox").value;
__rendered_path__17
How to determine the state of a checkbox using Javascript?
__rendered_path__20__rendered_path__20
var checkedP = window.document.getElementById("myCheckBox").checked;

Page 7
How to set the focus in an element using Javascript?
__rendered_path__1
<script> function setFocus() { if(focusElement != null) {
__rendered_path__5
document.forms[0].elements["myelementname"].focus(); } } </ script>
__rendered_path__7
How to access an external javascript file that is stored externally and not
__rendered_path__11
embedded?
__rendered_path__1
This can be achieved by using the following tag between head tags or between body
__rendered_path__5
tags.
__rendered_path__11
<script src="abc.js"></ script>How to access an external javascript file that is stored
__rendered_path__5
externally and not embedded? where abc.js is the external javscript file to be accessed.
__rendered_path__7
What is the difference between an alert box and a confirmation box?
__rendered_path__11
An alert box displays only one button which is the OK button whereas the Confirm box
__rendered_path__11
displays two buttons namely OK and cancel.
__rendered_path__28
What is a prompt box?
__rendered_path__1
A prompt box allows the user to enter input by providing a text box.
__rendered_path__28
Can javascript code be broken in different lines?
__rendered_path__11
Breaking is possible within a string
statement by using a backslash
\
at the end but
__rendered_path__11
not within any other javascript statement.
__rendered_path__5
that is ,
__rendered_path__5
document.write("Hello
\
world");
__rendered_path__5
is possible but not document.write
\
__rendered_path__11
("hello world");
__rendered_path__28
Taking a developer’s perspective, do you think that that JavaScript is easy to
__rendered_path__1
learn and use?
__rendered_path__1
One of the reasons JavaScript has the word "script" in it is that as a programming
__rendered_path__5
language, the vocabulary of the core language is compact compared to full-fledged
__rendered_path__5
programming languages. If you already program in Java or C, you actually have to
__rendered_path__11
unlearn some concepts that had been beaten into you. For example, JavaScript is a
__rendered_path__5
loosely typed language, which means that a variable doesn't care if it's holding a string,
__rendered_path__5
a number, or a reference to an object; the same variable can even change what type of
__rendered_path__11
data it holds while a script runs.
__rendered_path__5
The other part of JavaScript implementation in browsers that makes it easier to learn is
__rendered_path__5
that most of the objects you script are pre-defined for the author, and they largely
__rendered_path__5
represent physical things you can see on a page: a text box, an image, and so on. It's
__rendered_path__11
easier to say, "OK, these are the things I'm working with and I'll use scripting to make
__rendered_path__5
them do such and such," instead of having to dream up the user interface, conceive of
__rendered_path__5
and code objects, and handle the interaction between objects and users. With scripting,
__rendered_path__11
you tend to write a _lot_ less code.
__rendered_path__28
What Web sites do you feel use JavaScript most effectively (i.e., best-in-class
__rendered_path__1
examples)? The worst?
__rendered_path__93
The best sites are the ones that use JavaScript so transparently, that I'm not aware
__rendered_path__5
that there is any scripting on the page. The worst sites are those that try to impress me
__rendered_path__5
with how much scripting is on the page.
__rendered_path__7
How about 2+5+"8"?
__rendered_path__11
Since 2 and 5 are integers, this is number arithmetic, since 8 is a string, it’s
__rendered_path__11__rendered_path__5
concatenation, so 78 is the result.

Page 8
What is the difference between SessionState and ViewState?
__rendered_path__1
ViewState is specific to a page in a session. Session state refers to user specific data
__rendered_path__5
that can be accessed across all pages in the web application.
__rendered_path__7
What does the EnableViewStateMac setting in an aspx page do?
__rendered_path__11
Setting EnableViewStateMac=true is a security measure that allows ASP.NET to ensure
__rendered_path__11
that the viewstate for a page has not been tampered with. If on Postback, the ASP.NET
__rendered_path__5
framework detects that there has been a change in the value of viewstate that was sent
__rendered_path__5
to the browser, it raises an error
-
Validation of viewstate MAC failed.
__rendered_path__11
Use <%@ Page EnableViewStateMac="true"%> to set it to true (the default value, if this
__rendered_path__5
attribute is not specified is also true) in an aspx page.
__rendered_path__26
How to Accessing Elements using javascript?
__rendered_path__1
To do something interesting with HTML elements, we must first be able to uniquely
__rendered_path__5
identify which element we want. In the example
__rendered_path__11
<body>
__rendered_path__5
<form action="">
__rendered_path__5
<input type="button" id="useless" name="mybutton" value="doNothing" / >
__rendered_path__11
</ form>
__rendered_path__5
</ body>
__rendered_path__5
__rendered_path__5
We can use the "getElementById" method (which is generally preferred)
__rendered_path__11
document.getElementById("useless").style.color = "red";
__rendered_path__5
or we can use the older hierarchical navigation method,
__rendered_path__5
document.forms[0].mybutton.style.color = "blue";
__rendered_path__11
Notice that this uses the "name" attribute of the element to locate it.
__rendered_path__5
# Example of Accessing Elements in a DOM.
__rendered_path__5
__rendered_path__11
<script type="text/ javascript" >
__rendered_path__5
function showStatus() {
__rendered_path__5
var selectWidget = document.forms.beerForm.elements["beer"];
__rendered_path__5
var myValue = selectWidget.options[selectWidget.selectedIndex].value;
__rendered_path__11
alert('You drank a \"'+ myValue +"\"");
__rendered_path__5
return true;
__rendered_path__5
}
__rendered_path__80
</ script>
__rendered_path__5
__rendered_path__5
<form name="beerForm" action="">
__rendered_path__11
<select name="beer">
__rendered_path__5
<option selected="selected">Select Beer</ option>
__rendered_path__5
<option>Heineken</ option>
__rendered_path__11
<option>Amstel Light</ option>
__rendered_path__5
<option>Corona</ option>
__rendered_path__5
<option>Corona Light</ option>
__rendered_path__5
<option>Tecate</ option>
__rendered_path__11
</ select>
__rendered_path__5
__rendered_path__5
<input type="button" name="submitbutton" value="Drink"
__rendered_path__11
onclick="showStatus()" / >
__rendered_path__5
</ form>
__rendered_path__7
What looping structures are there in JavaScript?
__rendered_path__11__rendered_path__11
for, while, do-while loops, but no foreach.

Page 9
To put a "close window" link on a page ?
__rendered_path__1
<a href='javascript:window.close()' class='mainnav'> Close </ a>
__rendered_path__5
How to hide javascript code from old browsers that dont run it?
__rendered_path__1
Use the below speci
fied style of comments <script language=javascript> <!
--
javascript
__rendered_path__11
code goes here / / --> or Use the <NOSCRIPT>some html code </ NOSCRIPT> tags and
__rendered_path__17
code the display html statements between these and this will appear on the page if the
__rendered_path__11
browser does not support javascript
__rendered_path__22
How to comment javascript code?
__rendered_path__17
Use / / for line comments and
__rendered_path__17
/ *
__rendered_path__11
__rendered_path__11
*/ for block comments
__rendered_path__22
Name the numeric constants representing max,min values
__rendered_path__17
Number.MAX_VALUE
__rendered_path__17
Number.MIN_VALUE
__rendered_path__41
What does javascript null mean?
__rendered_path__1
The null value is a unique value representing no value or no object.
__rendered_path__11
It implies no object,or null string,no valid boolean value,no number and no array
__rendered_path__17
object.
__rendered_path__41
How do you create a new object in JavaScript?
__rendered_path__1
var obj = new Object(); or var obj = {};
__rendered_path__22
How do you assign object properties?
__rendered_path__17
obj["age"] = 17 or obj.age = 17.
__rendered_path__22
What’s a way to append a value to an array?
__rendered_path__1
arr[arr.length] = value;
__rendered_path__5
What is this keyword?
__rendered_path__1
It refers to the current object.
__rendered_path__22
To set all checkboxes to true using JavaScript?
__rendered_path__17
/ / select all input tags
__rendered_path__17
function SelectAll() {
__rendered_path__11
var checkboxes = document.getElementsByTagName("input");
__rendered_path__11
for(i=0;i<checkboxes.length;i++) {
__rendered_path__17
if(checkboxes.item(i).attributes["type"].value == "checkbox") {
__rendered_path__11
checkboxes.item(i).checked = true;
__rendered_path__11
}
__rendered_path__91
}
__rendered_path__94
}
__rendered_path__97
How to select an element by id and swapping an image ? ...
__rendered_path__91
<script language="JavaScript" type="text/ javascript" >
__rendered_path__11
function setBeerIcon() {
__rendered_path__17
__rendered_path__11
var beerIcon = document.getElementById("beerIcon");
__rendered_path__11
beerIcon.src = "images/ "+getSelectValue("beer")+".jpg";
__rendered_path__17__rendered_path__94
}

Page 10
</ script>
__rendered_path__1
__rendered_path__4
...
__rendered_path__1
__rendered_path__1
<img border="0" src="" id="brandIcon" alt="brand" / >
__rendered_path__1
__rendered_path__4
<select name="beer" id="beer" onChange="setButton();setBeerIcon();">
__rendered_path__1
<option value="--Select--">Select beer</ option>
__rendered_path__1
<option value="heineken">heineken</ option>
__rendered_path__4
<option value="sol">sol</ option>
__rendered_path__1
<option value="amstellight">amstellight</ option>
__rendered_path__1
<option value="coronalight">coronalight</ option>
__rendered_path__1
<option value="coronaextra">coronaextra</ option>
__rendered_path__4
<option value=""></ option>
__rendered_path__1
</ select>
__rendered_path__34
What does undefined value mean in javascript?
__rendered_path__4
Undefined value means the variable used in the code doesn't exist or is not assigned
__rendered_path__4
any value or the property doesn't exist.
__rendered_path__41
What is the difference between undefined value and null value?
__rendered_path__44
(i)Undefined value cannot be explicitly stated that is there is no keyword called
__rendered_path__1
undefined whereas null value has keyword called null
__rendered_path__4
(ii)typeof undefined variable or property returns undefined whereas typeof null value
__rendered_path__1
returns object
__rendered_path__34
What is variable typing in javascript?
__rendered_path__4
It is perfectly legal to assign a number to a variable and then assign a string to the
__rendered_path__4
same variable as follows
__rendered_path__1
example
__rendered_path__1
i = 10;
__rendered_path__1
i = "string";
__rendered_path__4
This is called variable typing
__rendered_path__41
Does javascript have the concept level scope?
__rendered_path__44
No. JavaScript does not have block level scope, all the variables declared inside a
__rendered_path__1
function possess the same level of scope unlike c,c++,java.
__rendered_path__34
What are undefined and undeclared variables?
__rendered_path__44
Undeclared variables are those that are not declared in the program (do not exist at
__rendered_path__1
all),trying to read their values gives runtime error.But if undeclared variables are
__rendered_path__1
assigned then implicit declaration is done .
__rendered_path__4
Undefined variables are those that are not assigned any value but are declared in the
__rendered_path__1
program.Trying to read such variables gives special value called undefined value.
__rendered_path__41
How to find the selected radio button immediately using the 'this' variable?
__rendered_path__44
<script>
__rendered_path__1
function favAnima
l(button) {
__rendered_path__4
alert('You like '+button.value+'s.');
__rendered_path__1
}
__rendered_path__101
</ script>
__rendered_path__1
<input type="radio" name="marsupial" value="kangaroo"
__rendered_path__1
onchange="favAnimal(this)">Kangaroo
__rendered_path__4
<br / ><input type="radio" name="marsupial" value="Opossum"
__rendered_path__1__rendered_path__1
onchange="favAnimal(this)">Opossum

Page 11
<br / ><input type="radio" name="marsupial" value="Tasmanian Tiger"
__rendered_path__1
onchange="favAnimal(this)">Tasmanian Tiger
__rendered_path__5
How to find radio button selection when a form is submitted?
__rendered_path__8
<script type="text/ javascript">
__rendered_path__1
function findButton() {
__rendered_path__1
var myForm = document.forms.animalForm;
__rendered_path__17
var i;
__rendered_path__1
for(i=0;i<myForm.marsupial.length; i++) {
__rendered_path__1
if(myForm.marsupial[i].checked) {
__rendered_path__1
break;
__rendered_path__17
}
__rendered_path__29
}
__rendered_path__32
alert("You selected \""+myForm.marsupial[i].value+"\".");
__rendered_path__1
}
__rendered_path__32
</ script>
__rendered_path__1
<form name="animalForm" action="">
__rendered_path__1
<input type="radio" name="marsupial" value="kangaroo" / >Kangaroo
__rendered_path__17
<br / ><input type="radio" name="marsupial" value="Opossum" / >Opossum
__rendered_path__1
<br / ><input type="radio" name="marsupial" value="Tasmanian Tiger" / >Tasmanian
__rendered_path__1
Tiger
__rendered_path__17
__rendered_path__1
__rendered_path__1
<input type="button" name="GO" value="GO" onclick="findButton()" / >
__rendered_path__58
How to disable an HTML object ?
__rendered_path__8
document.getElementById("myObject").disabled = true;
__rendered_path__5
To write messages to the screen without using "document.write()" ?
__rendered_path__8
Changing the contents of an element is a much better solution. When the method
__rendered_path__1
showStatus is invoked it will change the content of the span.
__rendered_path__1
...
__rendered_path__17
function showStatus(message) {
__rendered_path__1
var element = document.getElementById("mystatus");
__rendered_path__1
element.textContent = message; / / for Firefox
__rendered_path__17
element.innerHTML = message; / / for IE (why can't we all just get along?)
__rendered_path__1
return true;
__rendered_path__1
}
__rendered_path__32
...
__rendered_path__1
<span id="mystatus">Test. </ span>
__rendered_path__1
...
__rendered_path__58
How to Add new elements dynamically ?
__rendered_path__8
<html xmlns="http:/ / www.w3.org/ 1999/ xhtml" xml:lang="en" lang="en">
__rendered_path__1
<head>
__rendered_path__1
<title>t1</ title>
__rendered_path__17
<script type="text/ javascript">
__rendered_path__1
function addNode() {
__rendered_path__1
var newP = document.createElement("p");
__rendered_path__1
var textNode = document.createTextNode(" I'm a new text node");
__rendered_path__17
newP.appendChild(textNode);
__rendered_path__1
document.getElementById("firstP").appendChild(newP);
__rendered_path__1
}
__rendered_path__32__rendered_path__1
</ script>

Page 12
1280.jpg');
How to have an element invoke a javascript on selection, instead of going to a
How to have the status line update when the mouse goes over a link (The
Values stored in cookies may not have semicolons, commas, or spaces. You should use
ode the values, and "unescape()" to retrieve them.
document.cookie = "myTextBox="+ escape(myBox.value) + getExpirationString(
</ head>
__rendered_path__1
__rendered_path__4
<body onload="addNode();" style=" background: url('../ images/ Sand-
__rendered_path__1
background-color: yellow;">
__rendered_path__1
__rendered_path__1
<p id="firstP">firstP<p>
__rendered_path__4
__rendered_path__1
</ body>
__rendered_path__1
</ html>
__rendered_path__20
new URL: ?
__rendered_path__23
<script type="text/ javascript">
__rendered_path__4
function pseudoHitMe() {
__rendered_path__4
alert("Ouch!");
__rendered_path__1
}
__rendered_path__1
</ script>
__rendered_path__33
<a href="javascript:pseudoHitMe()">hit me</ a>
__rendered_path__1
support of the status line is sporadic)?
__rendered_path__20
<a href="javascript.shtml"
__rendered_path__23
onmouseover="window.status='Hi There!';return true"
__rendered_path__4
onmouseout="window.status='';return true">Look at the Status bar</ a>
__rendered_path__4
Look at the Status bar as your cursor goes over the link.
__rendered_path__1
How to create a popup warning box
__rendered_path__1
alert('Warning: Please enter an integer between 0 and 100.');
__rendered_path__20
How to create a confirmation box?
__rendered_path__4
confirm("Do you really want to launch the missile?");
__rendered_path__20
How to create an input box?
__rendered_path__23
prompt("What is your temperature?");
__rendered_path__63
How to setting a cookie with the contents of a textbox ?
__rendered_path__23
the handy "escape()" function to enc
__rendered_path__20
__rendered_path__73
/ / Sets cookie of current value for myTextBox
__rendered_path__4
function TextBoxOnchange() {
__rendered_path__1
var myBox = window.document.getElementById(myTextBox");
__rendered_path__1
}
__rendered_path__4
__rendered_path__1
/ / return a string like ";expires=Thu, 5 Jan 2006 16:07:52 UTC"
__rendered_path__1
function getExpirationString() {
__rendered_path__1
var exp = new Date();
__rendered_path__33
var threemonths = exp.getTime()+(120*24*60*60*1000);
__rendered_path__1
exp.setTime(threemonths);
__rendered_path__4
return ";expires="+exp.toGMTString();
__rendered_path__1
}
__rendered_path__1
__rendered_path__4
This is called from the event handler in the HTML.
__rendered_path__1__rendered_path__1__rendered_path__33__rendered_path__1__rendered_path__1
);

Page 13
__rendered_path__1
<input name="myTextBox" type="text" id="myTextBox"
__rendered_path__3
onchange="javascript:TextBoxOnchange()" / >
__rendered_path__6
How to getting values from cookies to set widgets?
__rendered_path__9
function getCookieData(labelName) {
__rendered_path__1
/ / from Danny Goodman
__rendered_path__3
var labelLen = labelName.length;
__rendered_path__1
/ / read cookie property only once for speed
__rendered_path__1
var cookieData = document.cookie;
__rendered_path__1
var cLen = cookieData.length;
__rendered_path__3
var i = 0;
__rendered_path__1
var cEnd;
__rendered_path__1
while (i < cLen) {
__rendered_path__3
var j = i + labelLen;
__rendered_path__1
if (cookieData.substring(i,j) == labelName) {
__rendered_path__1
cEnd = cookieData.indexOf(";",j);
__rendered_path__1
if (cEnd ==
-
1) {
__rendered_path__3
cEnd = cookieData.length;
__rendered_path__1
}
__rendered_path__45
return unescape(cookieData.substring(j+1, cEnd));
__rendered_path__1
}
__rendered_path__45
i++;
__rendered_path__1
}
__rendered_path__54
return "";
__rendered_path__3
}
__rendered_path__54
__rendered_path__3
/ / init() is called from the body tag onload function.
__rendered_path__1
function init() {
__rendered_path__1
setValueFromCookie("brand");
__rendered_path__3
setValueFromCookie("market");
__rendered_path__1
setValueFromCookie("measure");
__rendered_path__1
}
__rendered_path__45
__rendered_path__1
function setValueFromCookie(widget) {
__rendered_path__1
if( getCookieData(widget) != "") {
__rendered_path__3
document.getElementById(widget).value = getCookieData(widget);
__rendered_path__1
}
__rendered_path__45
}
__rendered_path__54
__rendered_path__1
/ / if you name your cookies the widget ID, you can use the following helper function
__rendered_path__3
function setCookie(widget) {
__rendered_path__1
document.cookie = widget + "=" +
__rendered_path__1
escape(document.getElementById(widget).value) + getExpirationString();
__rendered_path__3
}
__rendered_path__96
How to change style on an element?
__rendered_path__3
Between CSS and javascript is a weird symmetry. CSS style rules are layed on top of
__rendered_path__3
the DOM. The CSS property names like "font-weight" are transliterated into
__rendered_path__1
"myElement.style.fontWeight". The class of an element can be swapped out. For
__rendered_path__1
example:
__rendered_path__3
document.getElementById("myText").style.color = "green";
__rendered_path__1__rendered_path__1
document.getElementById("myText").style.fontSize = "20";

Page 14
You can add an event handler in the HTML definition of the element like this,
<input type="button" id="hitme" name="hitme" value="hit me" onclick="hitme()"
Or, interestingly enough you can just assign the event's name on the object directly
You can also use the W3C addEvventListener() method, but it does not work in IE yet:
document.getElementById("hitme4").addEventListener("click", hitme4, false);
document.getElementById("hitme4").removeEventListener("click", hitme4, false);
"onkeydown", "onkeypress", "onkeyup" events are supported both in ie and standards
-
or
-
__rendered_path__1
document.getElementById("myText").className = "regular";
__rendered_path__6
How to Handle Event Handlers?
__rendered_path__6
<script type="text/ javascript"><!
--
__rendered_path__6
function hitme() {
__rendered_path__1
alert("I've been hit!");
__rendered_path__1
}
__rendered_path__1
/ / -->
__rendered_path__21
</ script>
__rendered_path__1
__rendered_path__6
with a reference to the method you want to assign.
__rendered_path__1
__rendered_path__1
<input type="button" id="hitme2" name="hitme2" value="hit me2"/ >
__rendered_path__6
<script type="text/ javascript"><!
--
__rendered_path__1
function hitme2() {
__rendered_path__1
alert("I've been hit too!");
__rendered_path__1
}
__rendered_path__6
document.getElementById("hitme2").onclick = hitme2;
__rendered_path__1
/ / -->
__rendered_path__1
</ script>
__rendered_path__21
__rendered_path__1
You can also use an anonymous method like this:
__rendered_path__1
__rendered_path__6
document.getElementById("hitme3").onclick = function () { alert("howdy!"); }
__rendered_path__1
__rendered_path__1
__rendered_path__6
<input type="button" id="hitme4" name="hitme4" value="hit me4"/ >
__rendered_path__1
<script
type="text/ javascript"><!
--
__rendered_path__1
function hitme4() {
__rendered_path__6
alert("I've been hit four!");
__rendered_path__1
}
__rendered_path__1
if(document.getElementById("hitme4").addEventListener) {
__rendered_path__1
}
__rendered_path__6
/ / -->
__rendered_path__1
</ script>
__rendered_path__21
How to remove the event listener: ?
__rendered_path__1
<script type="text/ javascript"><!
--
__rendered_path__1
/ / -->
__rendered_path__21
</ script>
__rendered_path__1
__rendered_path__94
Key Events
__rendered_path__97
__rendered_path__6
based browsers.
__rendered_path__1__rendered_path__1__rendered_path__6__rendered_path__1__rendered_path__1__rendered_path__1__rendered_path__6__rendered_path__1
-

Page 15
__rendered_path__1
<script type="text/ javascript">
__rendered_path__3
function setStatus(name,evt) {
__rendered_path__1
evt = (evt) ? evt : ((event) ? event : null); / * ie or standard? */
__rendered_path__1
var charCode = evt.charCode;
__rendered_path__1
var status = document.getElementById("keyteststatus");
__rendered_path__3
var text = name +": "+evt.keyCode;
__rendered_path__1
status.innerHTML = text;
__rendered_path__1
status.textContent = text;
__rendered_path__3
}
__rendered_path__21
</ script>
__rendered_path__3
<form action="">
__rendered_path__1
<input type="text" name="keytest" size="1" value=""
__rendered_path__1
onkeyup="setStatus('keyup',event)"
__rendered_path__1
onkeydown="setStatus('keydown',event)"
__rendered_path__3
/ >
__rendered_path__1
<p id="keyteststatus">status</ p>
__rendered_path__1
</ form>
__rendered_path__40
How to make elements invisible ?
__rendered_path__43
Change the "visibility" attribute of the style object associated with your element.
__rendered_path__1
Remember that a hidden element still takes up space, use "display" to make the space
__rendered_path__1
disappear as well.
__rendered_path__1
__rendered_path__3
if ( x == y) {
__rendered_path__1
myElement.style.visibility = 'visible';
__rendered_path__1
} else {
__rendered_path__3
myElement.style.visibility = 'hidden';
__rendered_path__1
}
__rendered_path__61
How to set the cursor to wait ?
__rendered_path__43
In theory, we should cache the current state of the cursor and then put it back to its
__rendered_path__1
original state.
__rendered_path__1
document.body.style.cursor = 'wait';
__rendered_path__3
/ / do something interesting and time consuming
__rendered_path__1
document.body.style.cursor = 'auto';
__rendered_path__76
How to reload the current page ?
__rendered_path__43
window.location.reload(true);
__rendered_path__40
how to force a page to go to another page using JavaScript ?
__rendered_path__3
<script language="JavaScript" type="text/ javascript" ><!
--
__rendered_path__3
location.href="http:/ / newhost/ newpath/ newfile.html"; / / --></ script>
__rendered_path__76
How to convert a string to a number using JavaScript?
__rendered_path__43
You can use the parseInt() and parseFloat() methods. Notice that extra letters following
__rendered_path__1
a valid number are ignored, which is kinda wierd but convenient at times.
__rendered_path__3
parseInt("100") ==> 100
__rendered_path__1
parseFloat("98.6") ==> 98.6
__rendered_path__1
parseFloat("98.6 is a common temperature.") ==> 98.6
__rendered_path__3
parseInt("aa") ==> Nan / / Not a Number
__rendered_path__1
parseInt("aa",16) ==> 170 / / you can supply a radix or base
__rendered_path__76
How to convert numbers to strings using JavaScript?
__rendered_path__43__rendered_path__1
You can prepend the number with an empty string

Page 16
var mystring = ""+myinteger;
__rendered_path__1
or
__rendered_path__5
var mystring = myinteger.toString();
__rendered_path__1
You can specify a base for the conversion,
__rendered_path__1
var myinteger = 14;
__rendered_path__1
var mystring = myinteger.toString(16);
__rendered_path__5
__rendered_path__1
mystring will be "e".
__rendered_path__21
How to test for bad numbers using JavaScript?
__rendered_path__5
the global method, "isNaN()" can tell if a number has gone bad.
__rendered_path__5
var temperature = parseFloat(myTemperatureWidget.value);
__rendered_path__1
if(!isNaN(temperature)) {
__rendered_path__1
alert("Please enter a valid temperature.");
__rendered_path__5
}
__rendered_path__37
What's Math Constants and Functions using JavaScript?
__rendered_path__5
The Math object contains useful constants such as Math.PI, Math.E
__rendered_path__5
Math also has a zillion helpful functions.
__rendered_path__1
Math.abs(value); / / absolute value
__rendered_path__1
Math.max(value1, value2); / / find the largest
__rendered_path__1
Math.random() / / generate a decimal number between 0 and 1
__rendered_path__5
Math.floor(Math.random()*101) / / generate a decimal number between 0 and 100
__rendered_path__59
What's the Date object using JavaScript?
__rendered_path__62
Time inside a date object is stored as milliseconds since Jan 1, 1970.
__rendered_path__1
new Date(06,01,02) / / produces "Fri Feb 02 1906 00:00:00 GMT-0600 (Central
__rendered_path__5
Standard Time)"
__rendered_path__1
new Date(06,01,02).toLocaleString() / / produces "Friday, February 02, 1906 00:00:00"
__rendered_path__1
new Date(06,01,02)
-
new Date(06,01,01) / / produces "86400000"
__rendered_path__21
What does the delete operator do?
__rendered_path__62
The delete operator is used to delete all the variables and objects used in the program
__rendered_path__1
,but it does not delete variables declared with var keyword.
__rendered_path__59
How tp create Arrays using JavaScript ?
__rendered_path__62
<script type="text/ javascript">
__rendered_path__1
var days = new Array();
__rendered_path__5
days[0] = "Sunday"
__rendered_path__1
days[1] = "Monday"
__rendered_path__1
days[2] = "Tuesday"
__rendered_path__5
days[3] = "Wednesday"
__rendered_path__1
days[4] = "Thursday"
__rendered_path__1
days[5] = "Friday"
__rendered_path__1
days[6] = "Saturday"
__rendered_path__5
__rendered_path__1
document.write("first day is "+days[0])
__rendered_path__1
</ script>
__rendered_path__5
__rendered_path__1
This produces
__rendered_path__1
__rendered_path__5
first day is Sunday
__rendered_path__1
__rendered_path__1
A more compact way of creating an array is the literal notation:
__rendered_path__1__rendered_path__5
<script type="text/ javascript">

Page 17
var days = ["Sunday","Monday","Tuesday","Wednesday",
__rendered_path__1
"Thursday","Friday","Saturday"];
__rendered_path__3
document.write("first day is "+days[0]) </ script>
__rendered_path__1
This produces
__rendered_path__1
first day is Sunday
__rendered_path__10
How to delete an entry using JavaScript?
__rendered_path__13
The "delete" operator removes an array element, but oddly does not change the size of
__rendered_path__1
the array.
__rendered_path__1
<script type="text/ javascript">
__rendered_path__1
var days = ["Sunday","Monday","Tuesday","Wednesday",
__rendered_path__3
"Thursday","Friday","Saturday"];
__rendered_path__1
document.write("Number of days:"+days.length); delete days[4];
__rendered_path__1
document.write("<br / >Number of days:"+days.length);
__rendered_path__3
</ script>
__rendered_path__1
This produces
__rendered_path__1
Number of days:7
__rendered_path__1
Number of days:7
__rendered_path__37
How to use strings as array indexes using JavaScript?
__rendered_path__13
Javascript does not have a true hashtable object, but through its wierdness, you can
__rendered_path__1
use the array as a hashtable.
__rendered_path__3
__rendered_path__1
<script type="text/ javascript">
__rendered_path__1
var days = ["Sunday","Monday","Tuesday","Wednesday",
__rendered_path__3
"Thursday","Friday","Saturday"];
__rendered_path__1
__rendered_path__1
for(var i=0; i < days.length; i++) {
__rendered_path__1
days[days[i]] = days[i];
__rendered_path__3
}
__rendered_path__58
__rendered_path__3
document.write("days[\"Monday\"]:"+days["Monday"]);
__rendered_path__1
</ script>
__rendered_path__1
This produces
__rendered_path__3
days["Monday"]:Monday
__rendered_path__73
How to use "join()" to create a string from an array using JavaScript?
__rendered_path__13
"join" concatenates the array elements with a specified seperator between them.
__rendered_path__1
__rendered_path__1
<script type="text/ javascript">
__rendered_path__3
var days = ["Sunday","Monday","Tuesday","Wednesday",
__rendered_path__1
"Thursday","Friday","Saturday"];
__rendered_path__1
document.write("days:"+days.join(","));
__rendered_path__3
</ script>
__rendered_path__1
This produces
__rendered_path__1
days:Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday
__rendered_path__73
How to make a array as a stack using JavaScript?
__rendered_path__13
The pop() and push() functions turn a harmless array into a stack
__rendered_path__1
__rendered_path__1
<script type="text/ javascript">
__rendered_path__1
var numbers = ["one", "two", "three", "four"];
__rendered_path__3
numbers.push("five");
__rendered_path__1
numbers.push("six");
__rendered_path__1__rendered_path__3
document.write(numbers.pop());

Page 18
document.write(numbers.pop());
__rendered_path__1
document.write(numbers.pop());
__rendered_path__5
</ script>
__rendered_path__1
This produces
__rendered_path__1
sixfivefour
__rendered_path__12
How to shift and unshift using JavaScript?
__rendered_path__15
<script type="text/ javascript">
__rendered_path__1
var numbers = ["one", "two", "three", "four"];
__rendered_path__1
numbers.unshift("zero");
__rendered_path__1
document.write(" "+numbers.shift());
__rendered_path__5
document.write(" "+numbers.shift());
__rendered_path__1
document.write(" "+numbers.shift());
__rendered_path__1
</ script>
__rendered_path__5
This produces
__rendered_path__1
zero one two
__rendered_path__1
shift, unshift, push, and pop may be used on the same array. Queues are easily
__rendered_path__1
implemented using combinations.
__rendered_path__12
How to create an object using JavaScript?
__rendered_path__15
Objects can be created in many ways. One way is to create the object and add the fields
__rendered_path__1
directly.
__rendered_path__1
__rendered_path__5
<script type="text/ javascript">
__rendered_path__1
var myMovie = new Object();
__rendered_path__1
myMovie.title = "Aliens";
__rendered_path__1
myMovie.director = "James Cameron";
__rendered_path__5
document.write("movie: title is \""+myMovie.title+"\"");
__rendered_path__1
<
__rendered_path__67
This produces
__rendered_path__1
movie: title is "Aliens"
__rendered_path__1
To create an object you write a method with the name of your object and invoke the
__rendered_path__5
method with "new".
__rendered_path__1
<script type="text/ javascript">
__rendered_path__1
function movie(title, direct
or) {
__rendered_path__1
this.title = title;
__rendered_path__5
this.director = director;
__rendered_path__1
}
__rendered_path__67
var aliens = new movie("Aliens","Cameron");
__rendered_path__1
document.write("aliens:"+aliens.toString());
__rendered_path__1
</ script>
__rendered_path__5
This produces
__rendered_path__1
aliens:[object Object]
__rendered_path__1
__rendered_path__1
You can also use an abbreviated format for creating fields using a ":" to separate the
__rendered_path__5
name of the field from its value. This is equivalent to the above code using "this.".
__rendered_path__1
<script type="text/ javascript">
__rendered_path__1
function movie(title, director) {
__rendered_path__5
title : title;
__rendered_path__1
director : director;
__rendered_path__1
}
__rendered_path__67
var aliens = new movie("Aliens","Cameron");
__rendered_path__1
document.write("aliens:"+aliens.toString());
__rendered_path__1__rendered_path__5
</ script>

Page 19
This produces
__rendered_path__1
aliens:[object Object]
__rendered_path__4
How to associate functions with objects using JavaScript?
__rendered_path__8
Let's now create a custom "toString()" method for our movie object. We can embed the
__rendered_path__1
function directly in the object like this.
__rendered_path__12
__rendered_path__1
<script type="text/ javascript">
__rendered_path__1
function movie(title, director) {
__rendered_path__1
this.title = title;
__rendered_path__12
this.director = director;
__rendered_path__1
this.toString = function movieToString() {
__rendered_path__1
return("title: "+this.title+" director: "+this.director);
__rendered_path__12
}
__rendered_path__30
}
__rendered_path__33
var narnia = new movie("Narni","Andrew Adamson");
__rendered_path__1
document.write(narnia.toString());
__rendered_path__1
</ script>
__rendered_path__12
This produces
__rendered_path__1
title: Narni director: Andrew Adamson
__rendered_path__1
__rendered_path__12
Or we can use a previously defined function and assign it to a variable. Note that the
__rendered_path__1
name of the function is not followed by parenthesis, otherwise it would just execute the
__rendered_path__1
function and stuff the returned value into the variable.
__rendered_path__1
__rendered_path__12
<script type="text/ javascript">
__rendered_path__1
function movieToString() {
__rendered_path__1
return("title: "+this.title+" director: "+this.director);
__rendered_path__12
}
__rendered_path__30
function movie(title, dire
ctor) {
__rendered_path__12
this.title = title;
__rendered_path__1
this.director = director;
__rendered_path__1
this.toString = movieToString; / / assign function to this method pointer
__rendered_path__1
}
__rendered_path__33
var aliens = new movie("Aliens","Cameron");
__rendered_path__1
document.write(aliens.toString());
__rendered_path__12
</ script>
__rendered_path__1
This produces
__rendered_path__1
title: Aliens director: Cameron
__rendered_path__82
eval()?
__rendered_path__8
The eval() method is incredibly powerful allowing you to execute snippets of code during
__rendered_path__1
execution.
__rendered_path__1
<script type="text/ javascript">
__rendered_path__1
var USA_Texas_Austin = "521,289";
__rendered_path__12
document.write("Population is "+eval("USA_"+"Texas_"+"Austin"));
__rendered_path__1
</ script>
__rendered_path__1
This produces
__rendered_path__12
Population is 521,289
__rendered_path__102
What does break and continue statements do?
__rendered_path__8
Continue statement continues the current loop (if label not specified) in a new iteration
__rendered_path__1__rendered_path__12
whereas break statement exits the current loop.

Page 20
How to create a function using function constructor?
__rendered_path__1
The following example illustrates this
__rendered_path__6
It creates a function called square with argument x and returns x multiplied by itself.
__rendered_path__6
var square = new Function ("x","return x*x");
__rendered_path__11
What's Prototypes for JavaScript?
__rendered_path__1
Objects have "prototypes" from which they may inherit fields and functions.
__rendered_path__6
<script type="text/ javascript">
__rendered_path__6
function movieToString() {
__rendered_path__23
return("title: "+this.title+" director: "+this.director);
__rendered_path__6
}
__rendered_path__28
function movie(title, director) {
__rendered_path__23
this.title = title;
__rendered_path__6
this.director = director || "unknown"; / / if null assign to "unknown"
__rendered_path__6
this.toString = movieToString; / / assign function to this method pointer
__rendered_path__23
}
__rendered_path__28
movie.prototype.isComedy = false; / / add a field to the movie's prototype
__rendered_path__23
var officeSpace = new movie("OfficeSpace");
__rendered_path__6
var narnia = new movie("Narni","Andrew Adamson");
__rendered_path__6
document.write(narnia.toString());
__rendered_path__6
document.write("
__rendered_path__23
Narnia a comedy? "+narnia.isComedy);
__rendered_path__6
officeSpace.isComedy = true; / / override the default just for this object
__rendered_path__6
document.write("
__rendered_path__23
Office Space a comedy? "+officeSpace.isComedy);
__rendered_path__6
</ script>
__rendered_path__61
unescape(), escape()
__rendered_path__1
These are similar to the decodeURI() and encodeURI(), but escape() is used for only
__rendered_path__6
portions of a URI.
__rendered_path__23
__rendered_path__6
<script type="text/ javascript">
__rendered_path__6
var myvalue = "Sir Walter Scott";
__rendered_path__23
document.write("Original myvalue: "+myvalue);
__rendered_path__6
document.write("<br / >escaped: "+escape(myvalue));
__rendered_path__6
document.write("<br / >uri part: \"&author="+escape(myvalue)+"\"");
__rendered_path__23
</ script>
__rendered_path__6
__rendered_path__6
If you use escape() for the whole URI... well bad things happen.
__rendered_path__6
<script type="text/ javascript">
__rendered_path__23
var uri = "http:/ / www.google.com/ search?q=sonofusion Taleyarkhan"
__rendered_path__6
document.write("Original uri: "+uri);
__rendered_path__6
document.write("
__rendered_path__23
escaped: "+escape(uri));
__rendered_path__6
v/ script>
__rendered_path__61
decodeURI(), encodeURI()
__rendered_path__1
Many characters cannot be sent in a URL, but must be converted to their hex encoding.
__rendered_path__6
These functions are used to convert an entire URI (a superset of URL) to and from a
__rendered_path__23
format that can be sent via a URI.
__rendered_path__6
<script type="text/ javascript">
__rendered_path__6
var uri = "http:/ / www.google.com/ search?q=sonofusion Taleyarkhan"
__rendered_path__23
document.write("Original uri: "+uri);
__rendered_path__6
document.write("<br / >encoded: "+encodeURI(uri));
__rendered_path__6__rendered_path__23
</ script>