Home > Articles > Open Source > Ajax & JavaScript

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Scripting and Browser Concerns

In some instances, people are using old browsers with no JavaScript support or poor support for the script element itself, or have JavaScript disabled. Working around these issues requires some additional markup.

Hiding Scripts from Older Browsers

If you're using embedded JavaScript, some older browsers attempt to display whatever is contained within the script element as body text.

To avoid this, many people got into the habit of "commenting out" their scripts—in other words, using comment syntax to prevent the script from being displayed (see Example 3-11).

Example 3-11. Hiding a script with comments

<head>
          <script type="text/javascript">
          <!-- this hides the script from older browsers
           function newWindow() {foodWindow = window.open("images/photo.jpg",
           "foodWin", "width=250,height=188")}
           // End hiding script from old browsers -->
           </script>
</head>

Note the //. This is JavaScript syntax that enables you to write in a comment after that point that won't be displayed, either. The commenting used here will not prevent the script from operating normally in any browser that supports it.

Using the noscript Element

If you'd like to add some text so supporting browsers will display a message regarding script support, you can do so using the noscript element (see Example 3-12).

Example 3-12. Using the noscript element

<head>
           <script type="text/javascript">
           <!-- this hides the script from older browsers
           function newWindow() {foodWindow = window.open("images/photo.jpg",
           "foodWin", "width=250,height=188")}
           // End hiding script from old browsers -->
           </script>

           <noscript>Attention: Your browser does not support JavaScript or you
           have disabled JavaScript.
           </noscript>
</head>

Browsers that support scripting and do not have scripting disabled will not see the contents of the noscript element.

However, those browsers without JavaScript or, as in the case with Figure 3-13, browsers with JavaScript purposely turned off will display the text within the noscript element.

03fig13.gif

Figure 3-13 The noscript text within a browser with disabled JavaScript.

As you can see, the link is still intact, but the pop-up script will not work if JavaScript is disabled or unavailable.

  • + Share This
  • 🔖 Save To Your Account