Where to Add JavaScript Codes ?

By

Now that we’re logged in to JavaScript education we know where to go and how to write JavaScript code. It may seem insignificant to you, but the speed of your website may either slow down or, on the contrary, accelerate, depending on where you are adding important issues.

 

When you are faced with the question of where the JavaScript code is written, what you need to know is that we can write it into our HTML and pull it out if we want to create it at the top of the page, or we can create it right where we’re going to use it.

 

Where Can I Write JavaScript Codes ?

We can write JavaScript code basically in 3 different places. This is the web site that we will create with HTML in the head and the third part of the body part of the method if you can add all the JavaScript code we will insert into a file with a .js extension, and then we can write the file with the JavaScript extension that we have written in the page we will use.

 

Write JavaScript Code into Head Part

We can write JavaScript code into the head part of our website just like CSS. For this, it is enough to create script tags into the head part.

<head>
<script>
function firstFunction() {
document.getElementById("demo").innerHTML = "Hi. My name is Gokmen"; }
</script>
</head>
<body>
<p id="demo">Hi. My name is JavaScript</p>
<button type="button" onclick="firstFunction()">Click</button> 
</body>

In the example above, we created JavaScript codes in the head part and then clicked the JavaScript button to change the JavaScript.

 

Write JavaScript Codes Into Body Part

This time we do not necessarily want to write JavaScript code into the head part. In any case, depending on the case, we can add JavaScript codes in the body part momentarily. If the code that we added is in the body part, it will slowly load.

<body>
<script> 
function firstFunction() { 
document.getElementById("demo").innerHTML = "Hi. My name is Gokmen"; } 
</script>
<p id="demo">Hi. My name is JavaScript</p>
<button type="button" onclick="firstFunction()">Click</button> 
</body>

In the example above, when the button is clicked, the JavaScript code will work and then click the button.

 

Adding External JavaScript Files

In this section, I’ll talk about how to create a JavaScript page in HTML content without writing any JavaScript code. For this:

<head>
<script src ="filecreatedname.js"></script>
</head>
<body>
<p id="demo">Hi. My name is JavaScript</p> 
<button type="button" onclick="firstFunction()">Click</button> 
</body>

In this way, we create an external file with the function commands that exist in the above examples and save the extension as .js. Then, you can call this file with its name and enable it to execute the necessary JavaScript commands on this page.

 

Thanks to the examples above, we can perform the process of adding JavaScript codes in three different ways. It should be noted that this is a very simple but useful topic. Most of the time, the generated JavaScript code is added to an external file.

Leave a Comment

Your email address will not be published.

You may also like