My Profile

Monday, January 24, 2011

Hiding JavaScript from Older Browsers

Of course, any JavaScript you write will be intended for JavaScript-compatible browsers; however, the last thing you want is for someone using an ancient version of Mosaic to load your page and see the actual JavaScript code all over their screen.
You can avoid this by enclosing JavaScript within HTML comments. This hides the code from older browsers; JavaScript-aware browsers, such as Netscape, will interpret it as JavaScript. Here's an example of a <SCRIPT> tag with HTML comments: <!-- <SCRIPT language=JAVASCRIPT> document.write("I lost a buttonhole. "); </SCRIPT> -->
The key elements here are the comment begin and end tags: <!! and ->. These are standard HTML 2.0, and define the start and end of a comment.
Unfortunately, things aren't always that simple. Here are a few things to watch out for:
  • Some browsers will treat any greater-than sign (>) as the end of the comment. The decrement operator (-) can also cause problems.
  • Some browsers still won't recognize comments correctly, so your script may be displayed.
Because of problems like these, there is no ideal solution. The best way to avoid these problems is to use the <SCRIPT SRC> tag instead, although it isn't always the best solution. Another solution is the <NOSCRIPT> tag, which indicates content that will be ignored by JavaScript browsers. For example, this HTML will display only on non-JavaScript browsers: <NOSCRIPT> You're using a non-JavaScript browser. Please use the <a href="nojs.asp">Non-JavaScript version.</a> of this document. </NOSCRIPT>
For clarity, I won't use HTML comments in the examples through this guide. If you use these techniques on a Web page of your own, you may wish to add comments to support older browsers.

Workshop Wrap-Up

Although limited and still under development, JavaScript is a powerful tool to enhance a Web page. It offers features that are available to no other Web language. You've learned the following terms and concepts in this chapter:
  • What JavaScript is, and where it came from
  • The key differences between JavaScript and Java
  • How to include a JavaScript program within an HTML document, and keep it from displaying on older browsers
  • The key limitations and problems of JavaScript
  • The basics of writing a JavaScript application



** If you like this post please like our page in facebook


Hello there,

Thank you very much for visiting, commenting and following my blog on Google Friend Connect.
I have also followed back (No.: 33).

I like your blog, it is not hard to access, nice looking and informative.

I am also trying to increase the numbers of visitors to my blog.
But I can share with you :
1) Write good and popular things
2) Choose keywords most used by
other bloggers.
3) Visit other bloggers, comment and follow their blogs.

Let's keep in touch.

Post a Comment


Twitter Delicious Facebook Digg Stumbleupon Favorites More