ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

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

user-image
تم إضافة السؤال من قبل Faten Almajaly
تاريخ النشر: 2015/03/30
مستخدم محذوف‎
من قبل مستخدم محذوف‎

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
من قبل 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>

Ajmal Abdul Gafoor
من قبل Ajmal Abdul Gafoor , System Developer , Baud Telecom Company

the tag used for javascript in html is "script"

المزيد من الأسئلة المماثلة