Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

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

user-image
Question ajoutée par Faten Almajaly
Date de publication: 2015/03/30
Utilisateur supprimé
par Utilisateur supprimé

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
par 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
par Ajmal Abdul Gafoor , System Developer , Baud Telecom Company

the tag used for javascript in html is "script"

More Questions Like This