Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference in putting a javascript in a file and in a script tag?

What is the difference between the following two codes in an HTML file? If I add one more javascript file xyz.js after including the abc.js, is there any priority associated when the scripts are being used?

First code:

<script src="js/abc.js" type="text/javascript" language="javascript"> </script>

Second code:

<script language="javascript">
        /*same code of abc.js*/ 
</script
like image 578
sumit Avatar asked Jul 21 '11 14:07

sumit


People also ask

What is the difference between script and JavaScript?

TypeScript is an object-oriented programming language developed by Microsoft Corporation, whereas JavaScript is the programming language for the web. TypeScript is an open-source language to build large-scale web apps, whereas JavaScript is a server-side programming language that helps to develop interactive web pages.

Is a script tag needed when creating a JavaScript file?

In order to use JavaScript on an HTML web page, you must use the <script> tag to either write your JavaScript code directly in your HTML markup or to link to an external JavaScript file. This guide shows you how to use the <script> tag to link JavaScript to an HTML page.

Why is it a good idea to put your JavaScript either with script tags or an external file link near the end of the HTML body instead of in the head of the HTML?

Placing scripts in external files has some advantages: It separates HTML and code. It makes HTML and JavaScript easier to read and maintain. Cached JavaScript files can speed up page loads.

Why do we use script tag in JavaScript?

The <script> tag is used to embed a client-side script (JavaScript). The <script> element either contains scripting statements, or it points to an external script file through the src attribute. Common uses for JavaScript are image manipulation, form validation, and dynamic changes of content.


2 Answers

The primary difference is that the javascript file can be cached by the browser and network devices so the user doesn't have to download it on every page load.

So if you have 100k of javascript files, your visitor only needs to download them once. Otherwise, they'd have to download those same exact 100k every page load and visit.

This allow applies to inline and external CSS and images as well!!

Granted this is only the tip of the iceburg of caching and browser performance (Steve's book is one of the web 'bibles'):

http://yuiblog.com/blog/2006/11/28/performance-research-part-1/

http://www.yuiblog.com/blog/2007/01/04/performance-research-part-2/

http://www.stevesouders.com/

like image 93
Paul DelRe Avatar answered Sep 22 '22 15:09

Paul DelRe


What is the difference between the following two codes in an HTML file?

One requires an extra HTTP request, but gets cached. The other doesn't.

If I add one more javascript file xyz.js after including the abc.js, is there any priority associated when the scripts are being used?

Loading of external scripts is blocking. The first one will be loaded first. Then the second one will be loaded.

like image 27
Quentin Avatar answered Sep 21 '22 15:09

Quentin