New Posts
Home / Web Designing / JavaScript / How to Use JavaScript Tag.

How to Use JavaScript Tag.

How to Use JavaScript Tag.

In this lesson, we will learn how to use JavaScript tag. In below example: code is ok but the problem is JavaScript code is mixed with your HTML coding. So, Where JavaScript should go?

JavaScript Tag

<!doctype html>

<head>

<title> JavaScript Tag </title>

</head>

<body>

<p>This is a Paragraph.</p>

<script>

alert(“Hello World”);

</script>

</body>

</html>

I advise, Don’t do this because when you create big coding program then you will be confused, you code will harder to edit.

So instead of having this way, what refers to inline JavaScript we put the script in the separate file. You might do with CSS. It is easy to maintain and it is easy to use in cross multiple pages and also easy to read.

JavaScript’s separate file must be given .js extension.

Javascript separate file.

Myscript.js.

alert (“Hello World”);

We simply cut JavaScript code from HTML and put it in the separate file.  We do not need to copy across the script tag. We keep those in HTML pages. Because we need descript the link between our HTML page and JavaScript and we just use source attribute (src attribute) you might use an image tag in HTML.

JavaScript file is tag could be any file name with extension .js.

Right now, two files need to be side each other in folder and sub folder. Because src I mean the source attribute same rule that linking to image or style sheet file it could be the relative link, could be an absolute link and could be any one server in this course I am keeping in the same folder.

Now we have the great division between HTML and JavaScript.

What about the type attribute?

 type attribute

But in the following statement, there is a question about type, for HTML 4 or XHTML you can use type but in HTML5 you cannot use.

 type attribute html5

Let’s talk about little bit more Script tag itself.

When reading another people JavaScript, you are going to very often see this. Type attribute in your script tag. This is saying this is script JavaScript officially it could be whole range different options. If you leave it off. Then every browser will understand that it is JavaScript.   You have to use type attribute when you use HTML 4 or XHTML.

But if you use HTML 5 than it is optional and default.

Some code editors add type attribute automatically and some others plan if you do not have the freedom to leave it.

This is the question that where we put script tag?

Below example will work but this is not recommended because web browser will delay in rendering.

Location of the Script Tag.

<! doctype html>

<head>

<title>JavaScript Tag</title>

<script src=” myscript.js”></script>

</head>

<body>

<p> This is paragraph</p>

</body>

</html>

This will work smoothly.

<! doctype html>

<head>

<title>Hello World!</title>

</head>

<body>

<p> This is paragraph</p>

<script src=” myscript.js”>

</script>

</body>

</html>

CSS file is used into head but I advise use JavaScript file as bottom before closing </body> as you see.

 

About imran

My Name Mohammad Ramzan Bhutto. Live in District Jacobabad, Sindh, Pakistan. I have started blogging on Information Technology, my selected areas are Online Business, Digital Marketing, Make Money Online.

Check Also

Embedded Style Sheet

How to Use Embedded Style Sheet.

How to Use Embedded Style Sheet. Embedded Style Sheet or Internal style sheet is same …