dannyg.com logoSearch
 
JavaScript Mini-FAQ
by Danny Goodman, Author
Dynamic HTML: The Definitive Reference
JavaScript Bible

Last Update: 5 November 2002
 

Where is the online documentation for JavaScript?
Where is the official bug list for JavaScript?
Can JavaScript...?
Why does my code run in Browser X, but not in Browser Y?
How can I create a cross-browser modal dialog window?
How can I e-mail forms?
How do I script a visit counter?
After window.open(), how do I access objects and scripts in the other window?
How do I close another window?
Why do I get a script error in Internet Explorer when I use window.open() to open a new window?
How do I use JavaScript to password-protect my Web site?
What does the IE "Access Denied" error mean when accessing a new window?
What does the "Object not Found" error mean in IE (even though the object exists)?
How can I prevent others from reading/stealing my scripts or images?
Where can I find a longer FAQ?

To Top of Page
Q. Where is the online documentation for JavaScript and the Document Object Models?
A. Documentation for Microsoft's implementation of its core JavaScript language (called JScript) is at:
  <http://msdn.microsoft.com/library/en-us/script56/html/js56jslrfjscriptlanguagereference.asp>

Current Document Object Model reference documentation for Microsoft Internet Explorer is available at:
  <http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp>

Current JavaScript 1.5 docs (core language only) for Netscape are available at:
  <http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/>

Current Document Object Model reference documentation for Mozilla (Netscape) is available at:
  <http://www.mozilla.org/docs/dom/domref/>

To Top of Page
Q. Where is the official bug list for JavaScript?
A. No definitive cross-browser bug list exists. Only a masochist would attempt such a feat. While Microsoft maintains a closed bug system, Mozilla.org (creators of the engine behind Netscape 6 and later) lays it all out for the public at Bugzilla -- one of the best sanity checks on the Web.
To Top of Page
Q. Can JavaScript:
  • intercept the "Back" or "Forward" browser buttons?
  • read or write random text files on the local disk or on the server?
  • automatically print the current document without user interaction?
  • control browser e-mail or news reader windows and menus?
  • access or modify browser preferences settings?
  • capture a visitor's e-mail address without the user's knowledge?
  • quietly send me an e-mail when a visitor comes to my Web site?
  • launch client processes (e.g.,Unix sendmail,Win apps,Mac scripts)?
  • change the current browser window "chrome" (menus, bars, etc.)?
  • get rid of that dumb "JavaScript Alert:" line in alert dialogs?
  • insert my site into the Favorites/Bookmarks list wiihout user interaction?
A. No, however many of these items are possible via signed ActiveX controls in IE/Windows and signed scripts in Netscape 4 or later (requiring expertise in security certificates and associated server-based programming). Either approach explicitly asks the user's permission via dialog boxes, so don't expect to slip something past an unsuspecting user.
To Top of Page
Q. Why does my code run in Browser X, but not in Browser Y?
A. This is a frequently-asked question, for which thousands of answers apply. At the heart of the problem is the likelihood that you are facile with the development details of one browser platform, but not others. This particularly affects IE/Windows developers, who read the Microsoft Developer Network (MSDN) Web site as the Gospel. Well, it is, but only for MSIE/Windows, which implements tons of proprietary stuff that doesn't work outside of IE for Windows (a lot of it doesn't even work in IE for the Mac).

Add to that the difficulties surrounding the proprietary Netscape 4 stuff that was abandoned in Netscape 6 and later in favor of standards (um, recommendations) published by the World Wide Web Consortium (W3C), and it's a pickle.

The scripting and object models of today's browsers are far more complex than the early days. My books address the strategic questions you need to ask yourself before undertaking JavaScript and Dynamic HTML development projects.

If you have a pressing need for a solution, I recommend getting help from public forums where experts hang out. Two of my favorites are the JS-Jive Yahoo Group and the evolt.org mailing list. The comp.lang.javascript newsgroup can also help.

To Top of Page
Q. How can I create a cross-browser modal dialog window?
A. IE for Windows has a nice modal (and modeless) dialog windowing system (via the window.showModalDialog() method. But other browsers aren't so lucky. I've worked up a simulation of a modal window that works on both IE and Netscape (4 or later). You should read the full article about how it works, but also visit my update page, which has more recent code.
To Top of Page
Q. How can I e-mail forms?
A. It used to be a lot easier and more reliable in older browsers. It no longer is (unless you develop for a single browser platform and can verify that it works there). These days, you can miss too many users because their setups aren't configured in an ideal way. Better to search the Web for a sendMail service, which re-mails real form submissions back to you.
To Top of Page
Q. How do I script a visit counter?
A. At best, a client-side script can show the visitor how many times he or she has been to the site (storing the count in a local cookie). A count of total hits to the server requires a server-side CGI program.
To Top of Page
Q. After window.open(), how do I access objects and scripts in the other window?
A. Preserve the value returned by the window.open() method in a global variable -- it is the reference to the new window for access to its contents:

var newWind;
function makeNewWindow() {
    newWind = window.open("xxx","xxx","xxx"); // u fill in blanks
}

To access items in the new window from the original window, the 'newWind' variable must not be damaged (by unloading), because it contains the only reference to the other window you can use (the name you assign as the second parameter of open() is not valid for scripted window references; only for TARGET attributes). To access a form element property in the new window, use:

newWind.document.formName.elementName.propertyName

From the new window, the 'opener' property is a reference to the original window (or frame, if the window.open() call was made from a frame). To reference a form element in the original window:

opener.document.formName.elementName.propertyName

Finally, if the new window was opened from one frame in the main browser window, and a script in the new window needs access to another frame in the main browser window, use:

opener.parent.otherFrameName.document.formName. ...

To Top of Page
Q. How do I close another window?
A. A script in a main window may close only subwindows opened by window.open() from the the main window. User-opened windows are off-limits.

Going in the other direction, a subwindow may only attempt to close its opener. Even so, security mechanisms always force the display of a user confirmation alert before allowing the close() method to complete its task.

To Top of Page
Q. Why do I get a script error in Internet Explorer when I use window.open() to open a new window?
A. This problem has cropped up in Windows 95/NT due to the Registry losing track of one or two .dll files when applications are installed or uninstalled. The following solution from Microsoft was passed onto me by reader Scott Smith:
  1. Click Start, and then click Run.
  2. In the Open box, type the following line: regsvr32 actxprxy.dll
  3. Click OK, and then click OK again when you receive the following message: DllRegisterServer in actxprxy.dll succeeded.
  4. Click Start, and then click Run.
  5. In the Open box, type the following line: regsvr32 shdocvw.dll
  6. Click OK, then click OK again when you receive the following message: DllRegisterServer in shdocvw.dll succeeded.
  7. Shut down and restart your computer.

Fortunately, the problem occurs less frequently these days, as users have upgraded to newer Windows versions.

To Top of Page
Q. How do I use JavaScript to password-protect my Web site?
A. There are any number of schemes (I've used some myself). Most of them fail to deflect the knowledgeable JavaScript programmer, because no matter how you encode the correct password (e.g., bit shifting), both the encoding algorithms and the result have to be in the script -- whose source code is easily accessible. If you're only interested in keeping out casual visitors, this method may suffice.

A more secure way is to set the password to be the name or pathname of the HTML file on your site that is the 'true' starting page. Set the location to the value entered into the field. Entry of a bogus password yields an 'invalid URL' error.

If the protected pages need additional security (e.g., an infidel has managed to get the complete URL), you might also consider setting a temporary cookie on the password page; then test for the existence of that cookie upon entry to every protected page, and throw the infidel back to the password page.

To Top of Page
Q. What does the IE "Access is Denied" error mean?
A. The "Access Denied" error in any browser usually means that a script in one window or frame is trying to access another window or frame whose document's domain is different from the document containing the script. What can seem odd about this is that you get this error in IE for Windows frequently when a script in one window generates a new window (with window.open()), and content for that other window is dynamically created from the same script doing the opening. The focus() method also triggers the error.

The error can also occur if scripts try to access objects, properties, or methods that have been locked down by Microsoft's security platoon. For instance, the document.styleSheets.rules property used to be accessible in IE 5 and IE 5.5, but is not in IE 6.

For the new window problem, there is a bit of history associated with the problem and workarounds. For example, the problem occurs frequently when the scripts are being run from the local hard disk. You get a clue about the situation in the titlebar of the new window: It forces an about:blank URL to the new window, which is a protocol:domain that differs from wherever your main window's script comes from. If, however, you put the same main window document on a server, and access it via http:, the problem goes away.

There is a workaround for the local-only problem: In the first parameter of the window.open() method call, load a real document (even if it is a content-free HTML document) into the sub-window before using document.write() to generate content for the subwindow. The loading action 'legitimizes' the window as coming from the same domain as your main window's document.

(This solution does not affect scripts that load a page from a secure server into a separate window or frame. An http: protocol in one window and https: in the other--even if from the same server.domain--yield a security mismatch and "Access Denied." Setting the document.domain properties of both pages may solve the problem (but I am unable to test it for sure).)

For other situations (such as the document.styleSheets.cssRules problem, there are no workarounds, so you'll have to find another way around your task.

One more source of this error in IE occurs if you assign a too-long search string to a URL or a form (using the GET method) has lots of elements and data. The HTTP GET method has a built-in limit of approximately 512 characters. If you run into the problem, change the method to POST, which has no data length limit. But POST won't reflect the search string in the URL of the replacement page (in case you're expecting to parse that data as a way to convey data from one page to the next).

To Top of Page
Q. What does the "Object not Found" error mean in IE (even though the object exists)?
A. IE (especially for Windows) has a nasty habit of reporting errors incorrectly. The most common situation, which generates the "Object not Found" error, is when an event handler invokes a function. If there is a run-time error in the function, the error indication is that the function object is not found, and the line number (if it is anywhere near accurate -- unlikely when scripts are linked in from external .js files) points to the HTML containing the event handler attribute.

This situation makes debugging incredibly difficult without the help of Microsoft's Script Debugger or (my preference) running the script in another browser brand to get its take on the error. IE 5 for Macintosh and Netscape do a better job of pointing to script errors right off the bat.

To Top of Page
Q. How can I prevent others from reading/stealing my scripts or images?
A. Take it from a scripter since Day Zero: There is no foolproof way to prevent a determined user (not even a genius hacker) from viewing and capturing your source code and images. If the content can be served up to a browser, the same browser can be used (e.g., with JavaScript turned off) to bypass your scripted schemes to block the right click (wasted on the one-mouse Mac, anyway), overlay transparent content, or any other foolish waste of time.

Copyright your pages, watermark your images, and apply all the other non-techie ways of protecting your intellectual property. Forget the scripts.

To Top of Page
Q. Where can I find a longer FAQ?
A. http://www.faqts.com/knowledge_base/index.phtml/fid/53. Grab a cup of coffee first.

Google
Search dannyg.com*   Search WWW
*Search results are displayed on a google.com page, but links from search results bring you back to this site.
To Top of Page
Entire contents Copyright © 1996-2004 Danny Goodman. All Rights Reserved.

Valid XHTML 1.0!