CSS font stacks, media style sheets & web standards

Your banner ad here

WestNIC provides reliable web hosting services

25% off cpanel web hosting and reseller hosting deals. Promo: codestyle25off

Site navigation below

This FAQ is part of the Code Style Help and FAQ section. Join our premium content service for full access all FAQs, or choose the single FAQ by email option for premium answers.

Learning Javascript

Q: What is the difference between the script type and language attributes?

A: The script element's type attribute specifies the content type of the script, which should always be text/javascript, the standard Multipurpose Internet Mail Extensions (MIME) type for Javascript. The "text" part signifies that the script is essentially a plain text format. The "javascript" part specifies the scripting language.

The script element's language attribute is now deprecated, which means that it should no longer be used in HTML. The language attribute used to specify a particular version of the Javascript language required to run the script, to provide a fall-back mechanism for Web browsers that do not support more advanced scripting features. This was never a particularly good way to ensure script compatibility. Instead, you should use object, method and property detection to test the availability of specific scripting features before use. See Golden rules for Javascript for a guide to these defensive coding techniques.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

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.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

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 omitted

Premium Content: Follow this link for subscription information Get full access to all FAQs, subscribe now for $40
My Javascript code is showing!

Get the answer to this FAQ by email Get the full answer to this FAQ by email for $2.99
within 24 hours of clearance by PayPal.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

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.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

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 omitted

Premium Content: Follow this link for subscription information Get full access to all FAQs, subscribe now for $40
I can't get Javascript to check for null strings correctly!

Get the answer to this FAQ by email Get the full answer to this FAQ by email for $2.99
within 24 hours of clearance by PayPal.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

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.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: The onclick event on my button is not working!

A: In a situation like this the failure of your Javascript could originate at any number of levels. It is very important to see the whole system to understand what is going on, but these basic debugging steps should help.

  1. Is the Javascript code actually loaded into the page?
  2. Is the Javascript code parsed successfully?
  3. Is the event handler actually called?

premium content omitted

Premium Content: Follow this link for subscription information Get full access to all FAQs, subscribe now for $40
The onclick event on my button is not working!

Get the answer to this FAQ by email Get the full answer to this FAQ by email for $2.99
within 24 hours of clearance by PayPal.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

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.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

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 omitted

Premium Content: Follow this link for subscription information Get full access to all FAQs, subscribe now for $40
How do I get the difference between two dates in Javascript?

Get the answer to this FAQ by email Get the full answer to this FAQ by email for $2.99
within 24 hours of clearance by PayPal.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

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 omitted

Premium Content: Follow this link for subscription information Get full access to all FAQs, subscribe now for $40
How can I add 7 days to a date in Javascript?

Get the answer to this FAQ by email Get the full answer to this FAQ by email for $2.99
within 24 hours of clearance by PayPal.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

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.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

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 omitted

Premium Content: Follow this link for subscription information Get full access to all FAQs, subscribe now for $40
How can I create a file on the client side?

Get the answer to this FAQ by email Get the full answer to this FAQ by email for $2.99
within 24 hours of clearance by PayPal.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Add this page to your chosen social bookmarking service

Style warning - please read

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