DEV Community

loading...

SCRIPT TAG

5anthosh profile image Santhosh Kumar ・2 min read

This post was originally published on my blog, find original post here

Core JavaScript is platform independent,but capabilities of JavaScript depends on the its environment.

For server-side environment, you can execute the script with command like

santhosh@ubuntu-18-04:~$ node script.js

You need to install nodejs first to run server-side JavaScript

In browser, we have to attach a script to a webpage, JavaScript programs can be inserted into any part of an HTML document wiht help of the scripttag.

<!DOCTYPE html>
<html lang="en">
 <head>
 </head>
<body>
    <script>
        alert("Hello World");
    </script>
</body>
</html>

alert is browser specific function. script tag contains scripts which will be automatically executed when browser loads this webpage.

script tag contains few attributes.

Type

It is not required to specify the type in HTML5, usually it was type="text/javascript".

In old standard HTML4, it is required to put type of the script

External Scripts

only the simplest scripts are put into HTML. More complex ones reside in separate files.
Script files are attached to HTML with src attribute.

<script src="/path/to/script.js"></script>

we can attach third party script as well.

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

we can attach many scripts as much as we want.

<script src="/path/to/script1.js"></script>
<script src="/path/to/script2.js"></script>
<script src="/path/to/script3.js"></script>
<script src="/path/to/script4.js"></script>

If a script element has a src attribute specified, it should not have a script embedded inside its tags.

alert statement will not run.

<script src="/path/to/script4.js">
    alert("this will not run");
</script>

Discussion

pic
Editor guide