World class Hard Drive Recovery and renowned raid recovery services

WestNIC provides reliable web hosting services

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

Document output

Q: How can I make a printable page using Javascript?

A: You are strongly recommended to use CSS to achieve your print requirements, it is much better suited to this task and is more likely to be enabled in the users' browser. See the Media dependent style sheets FAQ for full details.

Q: How can I write Arabic characters in my Javascript?

A: The answer to this question depends on the medium you use for your text output. If you want to raise a Javascript alert, confirm or prompt containing Arabic, you must use a Unicode escape sequence: a back slash followed by a lower case letter u and a 4 digit hexadecimal number for the Unicode code point, as in the example below:

Premium Content: Follow this link for subscription information More details available to subscribers:
How can I write Arabic characters in my Javascript?

Page navigation

Q: How can I redirect based on a confirm response?

A: To create robust logical redirection for the Web you should use server side techniques; a simple, functionally transparent example is used in the noscript version of this demonstration. If confirmation is only intended as a gimmick and is not critical for navigation purposes, you can redirect the user using the window.location property.

Premium Content: Follow this link for subscription information More details available to subscribers:
How can I redirect based on a confirm response?

Q: How do I make sure redirects have the right "back" page?

A: Generally, it is better to use server side redirection using the HTTP 301 (moved permanently), 302 (found) or 303 (see other) status codes. These mechanisms should maintain appropriate back navigation with most common browsers.

Premium Content: Follow this link for subscription information More details available to subscribers:
How do I make sure redirects have the right "back" page?

Q: How can I generate a quiz results page?

A: This really depends how you have set the quiz up in the first place, but you could redirect the contestant to a results page and append the cumulative score variable to the query part of the URL...

Premium Content: Follow this link for subscription information More details available to subscribers:
How can I generate a quiz results page?

Q: How can I get the relative URL of a page?

A: The URL of the current page is represented by the Javascript window.location object. There are several components to the location object that represent parts of the URL. The relative URL of the page is given by the location.pathname property, plus any query or "search" part and fragment identifier or "hash". Not all URLs include a query and fragment identifier, so these properties may be null and need to be checked, as below.

Premium Content: Follow this link for subscription information More details available to subscribers:
How can I get the relative URL of a page?

Script execution

Q: How can I invoke Javascript without user input?

A: The usual way to invoke a script without a direct user action is to attach a function to the browser window's onload event handler. This event occurs when an HTML document is fully loaded and will automatically call your function. There are two ways to declare an onload event handler: add an onload attribute to the body element of the HTML, or assign the function in your Javascript, as below.

...
<body onload="myFunction();">
...
      
window.onload = myFunction;
      

Either approach will be sufficient. Don't use both or one will override the other.

Q: How should I write a script using timeouts?

A: The most important thing about using timeouts is to create a global variable, a "handle", by which you can clear the timeout if you need to.

Premium Content: Follow this link for subscription information More details available to subscribers:
How should I write a script using timeouts?

Q: How can I check if Javascript is enabled in my Perl script?

A: Since Perl code is executed on the server side, you need to submit an HTTP request to your Perl script that indicates whether Javascript is enabled on the client side. You could do this by dynamically writing the variable to a hyperlink URL, or perhaps a hidden form field, as below.

Premium Content: Follow this link for subscription information More details available to subscribers:
How can I check if Javascript is enabled in my Perl script?

Q: Is there a way to turn Javascript off in the HTML code?

A: If you are the author of the HTML code, then you control whether there is any Javascript in the document in the first place. If you want Javascript to run to start with, then stop functions from working later, you can set a boolean property that controls whether the functions execute or not, as below.

Premium Content: Follow this link for subscription information More details available to subscribers:
Is there a way to turn Javascript off in the HTML code?

Q: How can I toggle button images to enable and disable them?

A: This script sets a simple boolean variable called enabled when the page loads. A function called disable() is attached to an onclick event handler on the first image button, and an enable() function is attached to the second. The script includes alerts that notify the state of the system.

Premium Content: Follow this link for subscription information More details available to subscribers:
How can I toggle button images to enable and disable them?

Form validation

Q: Is it possible to filter non-Latin 1 characters?

A: It is possible to check for Latin 1 characters using Javascript, though the usual conditions apply; if Javascript is not enabled, no check will be made, so it is better to do this kind of thing on the server side, or double-up with both.

One relatively simple way to check for non-Latin characters would be to define a single string that contains all valid Latin 1 characters. Loop through each character in the input string and check it is in the reference string. See the example that follows.

Premium Content: Follow this link for subscription information More details available to subscribers:
Is it possible to filter non-Latin 1 characters?

Q: How can I provide secure authentication with Javascript?

A: Client side Javascript cannot be used to provide secure authentication for Web sites because all the code would be accessible to users. Anyone with a reasonable knowledge of Javascript could use the source code to decrypt the authentication details and gain access.

A robust authentication system can only be implemented using server side techniques. Methods vary according to the Web server you use, but lots of pointers are available from Google, search for HTTP authentication.

Help request

Use the form below to submit a help request or general enquiry about the Code Style Web site. Read our guidelines on asking the right questions first.

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.

Add this page to your chosen social bookmarking service

Style warning - please read

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