Javascript Frequently Asked Questions



Here are some various Javascript books and general reviews.
Javascript Sourcebook by McComb for Wiley---Good
Using Javascript by Reynolds & Wooldridge for Que---Fair
The Javascript Bible by Goodman---Good
Gesing and Schneider, JavaScript for the World Wide Web PeachPit press.
Flanagan. JavaScript, the Definitive Guide O'Reilly Nutshell


Here is a page with more Javascript examples and links.
Here is today'sDate and Time
Download a file with numerous date functions.


Here are some links to online tutorials and FAQs that may be useful.

Nick Heinle's javascript page
Javascript versus Jscript
Martin's tutorial.
Full information upon cookies can be found at the Persistent Cookie FAQ.

Questions & Answers

This is a list of frequently asked questions posted by users on the comp.lang.javascript newsgroup, and answers by myself and other experts from the newsgroup. The answers reflect the feelings and biases of the experts and may be blunt on certain issues. Some of the points may be domewhat dated and only apply to obsolete browsers.

Q-1. What is the difference between Javascript and Jscript?
A-1. Javascript is a scripting language created by Netscape to allow programmers to rapidly create code giving activity and functionality to webpages, above and beyond what is intrinsically allowed in HTML. Jscript is the common term used to refer to Internet Explorer's version of Javascript. Jscript in IE3 did not support the more interesting features of Javascript, and improperly used many of the common Javascript features. IE4 has more closely followed the features found in Netscape but varies greatly in the implementation of newer features such as DHTML.
Don Benish

Q-2. Can I use Javascript to see who is browsing to my pages?
A-2. No, unless the user has turned off the security features. This is on purpose for very good reasons. You as the user should not be harassed by merchants and spies who home in on the fact that you simply visited a certain site. No one wants their e-mail box filled with unwanted junk mail. If you browse to the more questionable sites, you surely don't want blackmailers making your tastes public. There was a way in Netscape 2.0 to have the browser send silent e-mail to the webmaster. This method was deleted in NN 2.01 and above. If the user has the security alert checkbox set to OFF in NN2.x, or above , then it is possible to send mail without his knowledge, but I always leave mine on. It is possible with CGI(Common Gateway Interface) or SSI(Server Side Includes) to record the domain name of the browser you are using, but I know of no way to record the particulars of your personal identity. Some people continue to insist that there is a way to collect all browser's identities. If you can supply the methods to me, I would very much appreciate knowing how it is done.
Don Benish

Q-3. Can you print out from Javascript?
A-3. Not from NN3+. For security reasons, the browser cannot automatically access windows functions. You cannot make the browser do things like push buttons on the browser(unless they were created by the html or Javascript program), print, access the computer's file structure, start other windows programs, or affect other non-browser functions. NN4+ has the ability to use signed scripts which allows the user to allow certain signatures to do special actions like printing. Browsers which do not have these special signatures set cannot perform these operations. IE4 has the ability to print using access to certain Com objects.
Don Benish

Q-4. Can I access functions in a frame or window from another frame or window?
A-4. Sure. The parent object is the way to access functions in other frames or windows.
Don Benish

Q-5. Is there a way to make the label on a button change when I put a mouse over the button?
A-5. Not on a button, however, you can use an Image as a link and change the Image dynamically. Create multiple Images resembling buttons and use them in reference links. Then use an onmouseover handler to change the Image.
Don Benish

Q-6. How do I make lines of text in alert boxes wrap properly?
A-6. Use \n where you want the line to break.
Don Benish

Q-7. How do I refresh my page?
A-7. Use the location object. Be aware that if you are refreshing the page you are on, any code after the refresh statement will not be executed.
Don Benish

Q-8. Can I preload images?
A-8. Sure. Use
Image1 = new Image;
Don Benish

Q-9. Can I use text files that can be written to the page by Javascript?
A-9. Sure. Netscape allows a feature called External files that can be loaded as a package of Javascript functions or objects. The way to use them is
<SCRIPT SRC="filename.JS"> 
default text or html if the load fails

Don Benish

Q-10. Can JavaScript Communicate With Java?
A-10. Yes. Within JavaScript you can create Java objects, call Java methods, and change parameter values in <EMBED> and <APPLET> tags. Built-in methods can generally be called directly by using the full reference of packagename.classname.methodname. Applets with public methods can be called with a reference to document.applet_name.method. Parameter values can be referenced with document.applet_name.parameter.
Daaron Dwyer

Q-11. Can Java Communicate With JavaScript?
A-11. Yes. Several methods exist within netscape.javascript.JSObject. From Java, you can call JavaScript methods, evaluate JavaScript expressions, retrieve JavaScript objects, and set the value of JavaScript objects.
Daaron Dwyer

Q-12. What are cookies and how do I use them?
A-12. The client can store relatively short bits of information in a file, called a cookie for no real reason. Cookies are set from the server via HTTP headers in the format:
Set Cookie: name=value; expires=date; path=directory_path;
domain=your_domain; secure
You set a cookie by assigning a "Set Cookie" string to the document.cookie object.
If an unexpired cookie is found for the domain and path, your browser responds with "Cookie: name=value; name=value ..." You can then assign the document.cookie object to a variable.

***Points to remember about cookies:
-Domain is the locale of the cookie's use and matches against the tail-- a domain of matches and (default is domain generating cookie).
-Path is a further granulation of use locale and compared only after passing domain tests. Path is pattern matched where "/foo" matches "/foobar" and "/foo/bar" (default is current request path).
-Using "secure" at the end will cause the cookie to only be sent in SHTTP or SSL sessions.
-maximum of 300 cookies, 4KB per cookie for entire name=value string, 20 cookies per specific domain
-multiple Set Cookie headers can be sent in a server reply
-cookies with the exact name and path overwrite the existing cookie entry. "/foo" will not overwrite "/foo/bar." You can delete cookies by sending the name and path with a past expires date.
-expires attribute indicates the "safe" purging date, but deletion is not required
Daaron Dwyer

Q-13.How can I use quote marks inside a string?
A-13. In two ways. Quotes can be escaped like this:
alert("The girl yelled \"Over there!\" The bear seemed to come out of no where.")
or you can use single quotes instead.
alert('The girl yelled "Over there!" The bear seemed to come out of no where.')

Don Benish

Q-14.I've seen a kind of JS-Alert asking for a stringinput like the username. Does anybody know how I can program this?

A-14. ret = prompt("message","default_text");
Don Benish

Q-15.When I use
anArray[1] = document.form1.InputFieldName;
Later, when I try to use anArray[1].name or [1].value, it's undefined

A-15. You have to first create the array using a constructor
var anArray = new Array();
Don Benish

Q-16.How can I get JS to write my IP address?
1) Can it be written to a text box in say a Form?
2) Can it be written to a page? (reg html)

A-16. Use
var a= location.hostname
You can use it anywhere you can use a string
Don Benish

Q-17.I get the following error message when I try to write to my form fields
Window.document.form.select1 has no property named 'selectedIndex'
A-17. Your problem may be that when you have more than one form you have to refer to it by name or by index.
Always name your forms and refer to them by name if possible.
Don Benish

Q-18.I am working with realaudio, and I was wondering if there was any way to detect if the user supports realaudio? I want to include sound effects as "onMouseOver" events, but it will really mess up users who don't have /want the realaudio support.
A-18. Per "Using Javascript"
"This is accomplished by very briefly opening a sub window and attempting to open a file of the given mime type in that window. If the plugin exists then the script returns true. This code only checks to see if your browser has any plugin that reads that mime type, not a specific plugin."
You can use the following code:
function probeplugin(mimetype){
var haveplugin=false;
var tiny =" ","teensy","width=1,height=1")
if(tiny !=null){
if( != null
return haveplugin
Don Benish

Q-19.I have a JavaScript function that works with NN, but not with MSIE. I know that it can't be made to work with MSIE, so I had the idea of trying to get it to display in Netscape only.
A-19. Use the navigator object to choose what to display where.
Here is a sample of how to use this.
function choose(){
if(navigator.appName == "Netscape") location = "Cv_start.htm";
else {location = "Ie_start.htm";}

Note!! Always give the program an option for the false proposition.
You can also have alternate methods for various versions of browsers as well.
Don Benish

Q-20.I've got a page with a number of links listed on it and I want to be able to determine what links people clicked on.
A-20.Make all your links set a variable like this:
var lindex1=0;
var lindex2=0;
<a href="blahblah" onClick="lindex1=1">
<a href="blahblah" onClick="lindex2=1">
var lindex= new Array(0,0,0,0,0,0,0,0);
<a href="blahblah" onClick="lindex[0]=1">
<a href="blahblah" onClick="lindex[1]=1">
Don Benish

Q-21.I am having problems getting one window appear in front of the others.
A-21.Look at the megaConverter.
I used this method
convWindow = open(blah,blah,blah);
this doesn't work right in IE3.
Don Benish

Q-22.From the original (un-named) browser window start a new window with the feature. From that window link to a file which will open in the origional (un-named) window. It seems easy but I don't seem to get it working just as a lot of others.
A-22. Look at the megaConverter.
The way I beat it was to create the first page with frames, one full sized and the other hidden. This allowed me to name that frame. Everything is then referenced back to that frame.
Don Benish
Another way to do it is is to use the property."mainframe"; //allows you to name any window that was not previously named in html or javascript.
thanks to

Q-23.Is there a command to split a string?
A-23.Use substring and indexOf methods.
firsthalf = whole.substring(0, whole.indexOf(':'));
secondhalf = whole.substring( whole.indexOf(':')+1,whole.length);
Don Benish

Q-24.Is there a way to change the text of a select box?
A-24. In the Javascript Sourcebook" Gordon gives this method:
formname.selectname.option[index].text = "Text";
You could change all the entries this way. The important feature is that you then reload the page to get it to display the new values.
Don Benish

Q-25.I'm looking for a way so that when the mouse cursor is brought over an image, a sound will play (perferably without opening another window).
A-25.Yes, it can be done in Navigator 3.0 or greater by using LiveConnect to manipulate LiveAudio plug-in settings. Check out:
(And for more excellent ideas, also check out:
You don't need to open other windows, or even use frames. (Just make sure you have both Java and JavaScript enabled.) A brief example:
<embed src = "mysound.wav" name="mysound" hidden=true autostart=false mastersound>
<a href="#" onMouseOver="" onMouseOut="document.mysound.stop()">
<img src="myimage.jpg">

Alternatively, you could remove the name attribute and refer to the embedded object like this:
John McNeirney

Q-26.What I want to do is have a person press a button in frame 1, which causes a javascript function in frame 2 to redraw the screen. That seems pretty simple just reload the page, but what if you want to pass some parameter to the function in frame 2 which causes it to redraw differently?
A-26.Send your new variables as arguments to your location command. You can then use to recover these arguments. Look at Mconv020  or jstest
to see how this is done.  The second link also demonstrates how to use .js files.
Don Benish

Q-27.When I have a child window opened I can look at it with my parent windpw to get a value. If the child window is not open, how do I make the function in the parent window not give an error message when it tries to look for the value.
A-27.In the body tag of the child window use onLoad and onUnload set or reset a vaue in the parent window. <BODY onLoad="opener.statdex=1" onUnload="opener.statdex=0">
Don Benish

Q-28.How do I pick a random image everytime anybody tries to load my page.
A-28.Dynamically write an image tag, inserting the randomly generted URL of the image. You can do this with the background also by writing the whole body tag.
Don Benish

Q-29.I have a function that determines a value and then writes a string depending on the value. When I place the function outside of a table it works fine, but when it is inside the table it doesn't work.
A-29.This is a known problem with javascript. The way to get around it is to dynamically write the whole html including the table.
Don Benish

Send comments or suggestions to:
Don Benish

Copyright © 1997-2009 Don Benish
Last Updated: