CSS font stacks, media style sheets & web standards

Your banner ad here

WestNIC provides reliable web hosting services

Top Canadian Hotels no booking fees from Victoria BC to Nova Scotia

Site navigation below

This FAQ is part of the Code Style Help and FAQ section. Join our premium content service for full access all FAQ 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

Sign up for premium content now, name your price Get full access to all FAQs, sign-up, name your price
My Javascript code is showing!

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

Sign up for premium content now, name your price Get full access to all FAQs, sign-up, name your price
I can't get Javascript to check for null strings correctly!

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

Sign up for premium content now, name your price Get full access to all FAQs, sign-up, name your price
The onclick event on my button is not working!

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

Windows 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.

Q: Can we hide the browser address bar?

A: It is not possible to hide the address bar from an existing browser window, only for a new "pop-up" window created by Javascript. Having said that, many people use pop-up blockers, so you should think carefully before you use new windows. Tabbed browsers may be configured to create a new tab in this case and some might show the address regardless. For example, Firefox and Opera show the current address at the top of the window, but it is not editable.

In any case, the address of the page will be accessible from the browser history list and by inspecting the source code of the page. For these reasons it is important to test across a number of target browsers. It might be better to use a lightbox-style overlay for dynamically generated content or possibly an iframe for page content.

The features of the new browser window are set by the third argument of the window.open() method, after the window's URL and target name reference, as below. Several other features can be configured but should be used with caution because they can severely restrict the usability of the new browser window.

premium content omitted

Sign up for premium content now, name your price Get full access to all FAQs, sign-up, name your price
Can we hide the browser address bar?

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

Sign up for premium content now, name your price Get full access to all FAQs, sign-up, name your price
How do I get the difference between two dates in Javascript?

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

Sign up for premium content now, name your price Get full access to all FAQs, sign-up, name your price
How can I add 7 days to a date in Javascript?

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

Sign up for premium content now, name your price Get full access to all FAQs, sign-up, name your price
How can I create a file on the client side?

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