Coping with Browsers without JavaScript


<script type="text/javascript">   document.write("Welcome to JavaScript!"); </script> <noscript>   Welcome to plain HTML! </noscript> 

According to recent surveys, up to 10% of users have JavaScript disabled, due to company policies, fear of browser security vulnerabilities, and other reasons. Therefore, you do have to make sure that your website can be used without JavaScript, as well.

One way to achieve this is to use the <noscript> element. Browsers with activated JavaScript ignore this element and its contents, whereas browsers without JavaScript show the element's contents. The preceding code (file noscript.html) generates the output shown in Figure 1.3 when a non-JavaScript browser (like the text browser Lynx) is used; Figure 1.4 shows a browser that supports the scripting language.

Figure 1.3. The preceding code in a browser without JavaScript.


Figure 1.4. The preceding code in a browser with JavaScript.


When JavaScript is used together with links, the following does not work fully as expected:

<a href="#" onclick="window.alert('Welcome to      JavaScript!'); ">click here</a> 


At first sight, we did everything right: Users with JavaScript get the modal window, users without JavaScript just click on a link to an empty text label; so nothing unexpected happens.

However, there is one little disadvantage: After the JavaScript link is clicked, a JavaScript-enabled browser shows the window but then still follows the link. Usually, that does not have an effect, but the browser scrolls to the top of the page because the text label has not been found.

To avoid this, just make sure that the code in the event handler returns false. This cancels all other effects the current event may have; in this case, the link will not be followed:

Avoiding that JavaScript-Enabled Browsers Still Follow the Link (link.html)

<a href="#" onclick="window.alert('Welcome to      JavaScript!'); return false;">click here</a> 




JavaScript Phrasebook(c) Essential Code and Commands
JavaScript Phrasebook
ISBN: 0672328801
EAN: 2147483647
Year: 2006
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net