Should You Do it With JavaScript

106 16
There are lots of things that can be done using JavaScript but not all of them should be done. Let's consider a few of the sorts of things where JavaScript isn't the best choice.

No browser has undergone bigger changes between versions than Internet Explorer has. There is also no other browser where any significant number of people continue to use old versions of the browser for a significant time after the release of a new version.

For these reasons it can sometimes be necessary to detect if your visitor is using an old version of Internet Explorer that cannot handle the way your modern web page works properly in the same way that modern browsers handle it. While feature sensing is the ideal way of detecting whether a particular JavaScript should run or not that doesn't help when it comes to rectifying problems due to the earlier IE version's lack of proper stylesheet support. To handle this situation we need to explicitly test for particular versions of Internet Explorer and the best way to do that is to use the conditional comments that Microsoft programs support. Trying to do the test using JavaScript browser sensing is not only inappropriate because of it not catering for those without JavaScript but also because browser sensing doesn't always work anyway.

The growing support for decent controls in stylesheets makes it less and less necessary to consider a JavaScript solution for achieving the appearance you want in your web page.

The type of menu that is normally referred to as a suckerfish menu (because the original example used the names of suckerfish as the menu entries) can work entirely without JavaScript in almost all popular browsers. The most popular browser where it doesn't work using just the CSS is IE6. Adding JavaScript to get a suckerfish menu to work in IE6 is a far superior solution to any of the other similar menu solutions that rely more heavily on JavaScript since the suckerfish menu will also work for a significant fraction of those who don't have JavaScript.

Similarly some people use JavaScript to set the alternate rows of a table to display in two different colours. As the appropriate CSS3 command is supported by more and more browsers it will become less necessary to provide JavaScript to perform this update to the table appearance.

While changing the way you write your page to reduce the reliance on JavaScript for the way the page looks will make your page look the same for more of your visitors, there can still be reasons for applying updates to the appearance using JavaScript since not everything can be easily done using CSS. So using JavaScript to assist in this area can still be appropriate provided that you make sure your page is still usable for those where the JavaScript isn't run.

Another area where the question of whether or not to use JavaScript can arise is with respect to form field validation. What you need to realise here is that client side validation using JavaScript and server side validation of the form after it is submitted serve two different purposes. The server side validation is essential regardless of whether you also include JavaScript validation since at least some of your visitors will not have JavaScript. The server side validation is there to meet your requirements in ensuring that the submitted data is actually valid for whatever it is supposed to contain. The JavaScript validation is there to make things more convenient for your visitors by providing them with feedback on what they might have entered incorrectly so that they can fix it before they submit the form. The server side validation ensures that whether or not the person filling out the form has the convenience of having the JavaScript tell them about invalid fields that any fields that are still invalid when the form is submitted will be detected.

There are not many things that can only be done with JavaScript and which can't be done any other way. Where you need something like that in your web page and where it is a nice to have but not critical element of your page then setting it up in such a way that those who do not have JavaScript cannot even see that it is there is the way to go. If the JavaScript does provide something essential to your site then you need to look at providing that information in some other way for those who don't have JavaScript. By building the solution that works without JavaScript first you can then decide whether the JavaScript alternative is even necessary at all. Unless it provides some benefit to those visiting your page it may be better to just go with the non-JavaScript way.

JavaScript can provide a great many benefits in making it easier for those visiting your page to interact with it. Those are the sorts of JavaScript that you should be using. Where the JavaScript is actually going to make your page harder to use the more knowledgeable of your visitors will turn it off and many of your visitors will move on looking for an easier to use site that provides the same information. Any JavaScript that does not provide a benefit for your visitors is therefore one that you should seriously consider not using.
Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.