Coding with JavaScript For Dummies
Book image
Explore Book Buy On Amazon

Many users are wary of enabling JavaScript support for sites because they don’t want to download a virus or have other bad things happen to their systems. In addition, people have become less receptive to using scripts because they sometimes do things the user doesn’t want, such as grab the user’s identity. When a user enables JavaScript for your application, it represents a trust relationship between the user and you.

Of course, the user needs to know that your site requires JavaScript in order to enable it. Consider these two options:

Use the


However, some people have complained that the

  Using the NoScript Tag


Using the NoScript Tag

This approach is quite simple. When you try the example, you see one of two messages. If you have JavaScript enabled, you see “You have JavaScript enabled!” Otherwise, you see “Please enable JavaScript to use this page!” Try disabling JavaScript support in your browser and refreshing this page to see the alternative message.

Use styles for JavaScript alert

The styles approach relies on the use of Cascading Style Sheets (CSS) to create a condition where a message is displayed when scripting is disabled. The problem with this approach is that not every browser supports it. Yes, newer browsers do support this technique just fine, but if you encounter users with older browsers, your application could experience problems. The following example shows how to use this technique.

  Using Styles


Using Styles

Please enable JavaScript to use this page!

This technique relies on a simple trick. When scripting is enabled, the first script creates a style for the first

tag that hides the information in that paragraph by adding the display: none style. The presence of scripting support also lets the second script run, which displays the “You have JavaScript enabled!” message.

When scripting is disabled, the first script can’t run, so there’s no special style for the

tag with an id of NoJS. This means that the “Please enable JavaScript to use this page!” message is displayed. However, because there’s no scripting support, the second script also fails, which means you won’t see the JavaScript-enabled message. Try this example by enabling, then disabling, JavaScript support on your browser.

About This Article

This article is from the book:

About the book authors:

Chris Minnick is an author, trainer, and web developer who has worked on web and mobile projects for both small and major businesses. Learn more at Eva Holland is an experienced web developer, tech trainer, and coauthor of Coding with JavaScript For Dummies. With Chris Minnick, she founded WatzThis?, a company focused on training and course development.

This article can be found in the category: