Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

Which tag do we use insert a JavaScript in HTML page?

user-image
Question added by Faten Almajaly
Date Posted: 2015/03/30
Deleted user
by Deleted user

Basm allah alrahman alrahim

 

<script>

 

but where that's the question ?

 

in between <head></head> or before end </body> tag

 

your codes must be at the end

 

jquery JavaScript or bootstrap JavaScript files or similar can be at the end or between head tags

 

there is another question why we put our codes at the end before </body> ?

 

because all html tags must be loaded first and CSS styles must be loaded and applied at those html tags then we can apply our codes from script tag or from external file at all html tags , if we put our codes at the head start and end tag they will not applied [ some times ] because our tags still not rendered [ shown ] at the browser yet , for example how can you apply the following JavaScript [ document.getelementById ] before the id appear at any tag

 

Example

1 - this a good way to run your javascript

 

<!DOCTYPE html>

<html>

   <body>

       <p id="demo" onclick="myFunction()">

          Click me to change my HTML content (innerHTML).

      </p>

<script>

function myFunction()

   {

    document.getElementById("demo").innerHTML = "Paragraph changed!";

   }

   </script>

   </body>

</html>

 

2 - this a bad way to run your javascript

 

<!DOCTYPE html><html>

<head>

<script>

function myFunction()

{

    document.getElementById("demo").innerHTML = "Paragraph changed!";

}

</script>

</head>

<body>

<p id="demo" onclick="myFunction()">

Click me to change my HTML content (innerHTML).

</p>

</body>

</html>

 

Rehan Farooq
by Rehan Farooq , WEB DEVELOPER/DIGITAL MARKETING EXPERT , Upwork

To insert a JavaScript in html page,

 

<script type="text/javascript">

Your Javascript code

</script>

 

To link the JavaScript you will need to use the following:

 

<script type="text/javascript" src="Your Script Source Link"></script>

<...>

this is tag is used in html

example. <p>

Ajmal Abdul Gafoor
by Ajmal Abdul Gafoor , System Developer , Baud Telecom Company

the tag used for javascript in html is "script"

More Questions Like This