CSS font stacks, developer FAQs & 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.

Document properties and 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.

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

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 omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
How can I write Arabic characters in my Javascript?

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

Q: Can I use Javascript to get entrance keywords from a search engine?

A: Yes, it is possible to get the referring search engine query, if the browser exposes the document.referrer property, which is the URL of a page that links to the current page. For security reasons, some browsers allow users to control whether referring page information is available. The complete example below solves several significant technical and other security issues to obtain a clean, plain text copy of the original keyword phrase:

  1. Find the domain name of the referring search engine.
  2. Look-up the query parameter name used by each search engine.
  3. Identify and extract the query value from the referring results page URL.
  4. Decode the URL encoded query to plain text.
  5. Clean the query to remove potentially malicious markup.
  6. Use the entrance keywords to set the style of the page.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
Can I use Javascript to get entrance keywords from a search engine?

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

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. An example confirmation form is included in the noscript fallback for the main Javascript implementation detailed below.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
How can I redirect based on a confirm response?

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

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 omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
How do I make sure redirects have the right "back" page?

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

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 omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
How can I generate a quiz results page?

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

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 omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
How can I get the relative URL of a page?

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

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.

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

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. The example below defines two functions delayAction() that sets the timeout to execute in 500 milliseconds and cancelAction() which aborts the action. The script inserts two action buttons into the page to trigger these function calls.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
How should I write a script using timeouts?

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

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 omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
How can I check if Javascript is enabled in my Perl script?

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

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 omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
How can I toggle button images to enable and disable them?

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

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 omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
Is there a way to turn Javascript off in the HTML code?

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

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. The working example below includes utilities to generate the reference character set and non-Latin characters to test.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
Is it possible to filter non-Latin 1 characters?

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

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.

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

Javascript application development

Q: How should I design a GUI for a Javascript app?

A: This is a big question and the answer has more to do with user experience than technical implementation; what tasks do users want to do with your user interface and how will it present itself to them? List out all the things users will want to do in priority order from their point of view, not yours. Use pen and paper to sketch out the user interface for each of these use cases independently of each other. What information will the user need? What input will they need to make? How will the system respond in the expected outcome and in error cases? Are there common elements amongst them? Re-draw the screens with common elements merged and separate tasks available in menus, tabs or buttons.

Show your design sketches to other people, ideally people who will use the system, to check and refine the interface and how it should behave. By testing with real users you can discover useful additions that you might not have thought of.

Once you have final sketches for the interface, build it as a separate series of pages in standard HTML and CSS first. It is far easier to test and refine your design and layout in hard code than re-generate code using Javascript. You may find find this format will work well enough for people with Javascript switched off.

Again, show your HTML pages to end users and get feedback before transcribing the markup to Javascript. As far as possible use the static HTML as the basis of your application and enhance its behaviour with Javascript. You may find that Javascript event handlers for specific actions will improve performance without re-generating the whole user interface using Javascript.

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