Javascript - Inline Code VS External Files

Inline Code

<html>
<head>
<script type="text/javascript">
function a()
{
alert('hello world');
}
</script>
</head>
<body>
:
:
</body>
</html>

External Files

<html>
<head>
<script type="text/javascript" src="/scriptone.js">
</script>
</head>
<body>
:
:
</body>
</html>

scriptone.js

function a()
{
alert('hello world');
}

As you can see from the above example,  what is the advantage to put all script in one file and just call it from html(External Code) instead of just write it in html(inline code).?

The answer is here :

  1.  By using external code, the javascript code is much more maintainability :- javascript Code that is sprinkled throughout various HTML pages turns code maintenance into a problem. It is much easier to have a directory for all javascript files so that developers can edit JavaScript code independent of the markup in which it is used.
  2. Caching - browser cache all externally linked JavaScript files according to specific settings, meaning that if two pages are using the same file, the file is download only once. This ultimately means faster page load times.
  3. Future-proof - By including JavaScript using external files, there is no need use the XHTML. The syntax to include external files is the same for both HTML and XHTML.


By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

Popular posts from this blog

How to create zip file to download in JSP- Servlet

How to create DataGrid or GridView in JSP - Servlet

Pinging in ASP.NET Example