Avoid Javascript whenever possible


Javascript is the most popular programming language used on internet. It is also extended in many other platforms for system administration/scripting/macro purposes. For example, WSH (Windows Script Hosting) supports Javascript.

The reason that it should be avoided whenever there is an alternative is that most browsers have their own Javascript implementation which might not be compatible. Most of the front-end enginneers nowadays need to consider the difference of Javascript in different browsers, such as IE, Firefox, Chrome etc.

Even if the Javascript code makes no difference on different browsers, the browsers have the configuration settings to disable the javascript. That is why <noscript>…</noscript> HTML tag is for.

Below is a simple example of Javascript that used to submits a search query, which should be replaced by general form submission.

1
2
3
4
5
6
7
8
<script language='Javascript'>
function updateURL(s)
{
    location.href='?s=' + s;
}
</script>
<input id="s" name="s" type="text" />
<input onclick="javascript:updateURL(document.getElementByID("s").value);" type="button" value="search" />
<script language='Javascript'>
function updateURL(s)
{
    location.href='?s=' + s;
}
</script>
<input id="s" name="s" type="text" />
<input onclick="javascript:updateURL(document.getElementByID("s").value);" type="button" value="search" />

This can be easily replaced by using HTML form technique:

1
2
3
4
<form method="GET">
<input name="s" type="text" /> 
<input type="submit" value="search" />
</form>
<form method="GET">
<input name="s" type="text" /> 
<input type="submit" value="search" />
</form>

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
249 words
Last Post: Quick Examples on SED
Next Post: BASH For Loops

The Permanent URL is: Avoid Javascript whenever possible

2 Comments

  1. dankred2
  2. Hois

Leave a Reply