World class data recovery software and renowned raid recovery services

WestNIC provides reliable web hosting services

Ahosting.biz reseller hosting, managed dedicated server with 24/7 support

Site navigation below

This FAQ is part of the Code Style Help and FAQ section. Use the help request form below if your question is not answered here, but make sure you are asking the right question first.

Subscribe to this FAQ: RSS news feed

FAQ search

Learning Javascript

Q: Can I use a Javascript file stored as .jds?

A: On the Web, the file extension you use for Javascript is not as important as the Content-Type header it is served with. With Apache, add this line to your .htaccess configuration file:

AddType "application/x-javascript; charset=iso-8859-1" jds
          

It is certainly best to separate your Javascript from your HTML like this in most cases. Firstly, it makes the script easier to edit and maintain, and means that any number of HTML pages can reference the same script. Normally, a shared external script will only be downloaded once for each visitor, which speeds up delivery of your page.

Javascript problems

Q: My Javascript code is showing!

A: It is conventional to put HTML comments around Javascript element content to hide it from browsers, like so ...

Premium Content: Follow this link for subscription information More details available to subscribers:
My Javascript code is showing!

Q: I can't get your script to work!

A: One of the key steps in externalising scripts is to ensure that all script file references are accurate.

Is the external file reference correct?

The Code Style site uses relative URL paths with a leading forward slash, /; this means the URL is relative to the root level of the domain, not the current directory. If you are adapting Code Style examples on a local file system, you will need to change these file references accordingly.

The following examples use stylesheet references, but the same conditions apply to all external files including scripts and images.

./Style.css
Refers to the file Style.css in same directory as the HTML source, the "current" directory.
styles/Style.css
Refers to the file Style.css in a subdirectory of the current directory called styles.
../styles/Style.css
Refers to the file Style.css in a directory called styles at the same hierarchical level as the current directory, i.e. in parallel to it.

Has the script file loaded?

To check if an external script file has loaded, add the following code to the top of the file and reload the HTML:

// Debugging
alert('Script loaded.');
          

This should raise a visible dialogue box if the script loads (you may need to clear your browser's cache to force a reload). Once you're sure the script has loaded, put other temporary alerts into the script to find out which parts execute and which fail.

Q: I can't get Javascript to check for null strings correctly!

A: It would help to see a fragment of the code. One thing to watch out for is the use of a single assignment equals instead of the double comparison equals...

Premium Content: Follow this link for subscription information More details available to subscribers:
I can't get Javascript to check for null strings correctly!

Q: I get "Done, but with errors on page" in IE!

A: The message "Done, but with errors on page" is an Internet Explorer error message that means there was a problem with one or more of the Javascript statements on the page. Normally you will see a warning icon alongside the error message in the status bar. If you double click on the icon, a status message will open to give details of the problem, which may help you fix the errors.

Window and frame communication

Q: Can I get the URL of any other browser window?

A: Web browsers' Window object references only extend as far as windows in the same frameset or "child" windows spawned from a specific document. It would be a user security violation if a script could read any other window's URL because this information could be communicated back to the script's host. For instance, you could append the URL to the query parameter of a second page on the script's host, redirect to that URL and log or process the request.

Working with dates in Javascript

Q: How do I get the difference between two dates in Javascript?

A: There are two elements to this question, first to find the difference between two dates and second to format the result. The code below creates three variables to represent time intervals in milliseconds, then creates two dates with specific times on the same day. This could be adapted for any two dates, including the current time using the basic Date() constructor.

Premium Content: Follow this link for subscription information More details available to subscribers:
How do I get the difference between two dates in Javascript?

Q: How can I add 7 days to a date in Javascript?

A: The example below creates a new Date object and a set of constants to calculate the number of milliseconds in 7 days. The 7 day figure is added to the original time and used to create a second Date object.

Premium Content: Follow this link for subscription information More details available to subscribers:
How can I add 7 days to a date in Javascript?

File security constraints

Q: Can I get the file size on the client side?

A: It is not possible to access files on a person's computer using Javascript through a Web browser, or to read the properties of files. It would be a serious security and privacy hazard if Javascript code in a Web page could read files on your computer because the information could be sent back to the site via a form or other mechanisms. For these reasons the possibility of file access was excluded when the Javascript language was designed.

Other client side solutions such as ActiveX and Java applets require special security consent from the user, which many people are reluctant to grant, and only make the job more complicated and risky from their point of view. Not all Web browsers are configured to support ActiveX or Java.

The only effective way to check the size of a file for upload is on the server side. Your server side form handler must process the file and this is the best place to apply a limit to the number of bytes you will accept.

Q: How can I create a file on the client side?

A: It is not possible to create files on the client's computer using Javascript. This type of action is excluded from the Javascript language because it would be a serious security hazard. The only form of local file storage available through Javascript is cookies, whose behaviour and contents are strictly prescribed.

Of course it is possible to use Javascript to generate file content in a Web browser window and invite people to save the contents. Internet Explorer and Firefox users can use the File menu Save as... option directly, but Opera and other browser users may have to copy the text into another application to save it.

Premium Content: Follow this link for subscription information More details available to subscribers:
How can I create a file on the client side?

Help request

Use the form below to submit a help request or general enquiry about the Code Style Web site. Before you write read the guidelines on asking the right questions, and check this page for periodic updates.

Information: Your email address will not be mis-used. If you include your address you may be sent a personal reply, you will not be added to any mailing list unless you request it. Read the site privacy statement for details.

Style warning - please read

Home · CSS · Java · Javascript · HTML · Help · Log