How to Place Script Tag in JavaScript

How to Place Script Tag in JavaScript?

Introduction

we are here to know about How to Place Script Tag in JavaScript but first, we learn about javascript. JavaScript code doesn’t care where it exists. It may be mixed in with—that is, embedded in—the HTML that makes a Web page. Or, similar to an outside CSS file, it may be placed in a separate file that is loaded by the HTML file. In best cases, it’s finest to have an external JavaScript file; for certain of the same reasons, it’s best to have an external CSS file. On the other hand, we still want to know how to place JavaScript inside our HTML file.

Description

  • In JavaScript, Script tags can be put in into three places:

In the middle of the two BODY tags,
In between the two HEAD tags,
For a link to an external file, also in the HEAD section.

By way of we know, the order of HTML code is significant, as it controls the order of elements on the page. We put the headline above the text in the HTML if we want the headline to appear above the text. But JavaScript doesn’t do the equal way. All of our JavaScript functions, for instance, are loaded into memory when the page loads. Somewhere the code can be, the browser finds it and stores it with the time the page is ended loading. All the JavaScript code stays in memory, prepared to execute, for as long as the page is displayed when the page is loaded.

We can with authorization put JavaScript code almost anywhere in the HTML file—in the head section, at the start of the body section, anywhere in the middle of the body section, or at the finish of the body section. We could, if we wanted to be willful, sprinkle different JavaScript functions all over our HTML file in diverse places. The browser would category it all out.

Normally, the greatest place for scripts, and yet, is at the end of the body section. These assurances that CSS shaping and image display won’t get delayed while scripts are loading. We must enclose the JavaScript code between and tags when we embed blocks of JavaScript in HTML. Following are two functions surrounded by the tags.

  1. li>
  2. function sayHi() {
  3. alert (“Hello world!”);
  4. li>
  5. function sayBye() {
  6. alert (“Buh-bye!”);
  7. li>
  8. li>

We can enclose any amount of code—with any number of functions—in the middle of the and tags. I mentioned before, we can have different sections of code scattered all over our HTML file if we want. But each section has to be enclosed between the and tags.

As for most purposes, coders prefer to have all or most of their JavaScript code in a separate JavaScript file, then have the browser load this external file at the same time it’s loading the HTML file.

A JavaScript file is, similar to HTML and CSS files, a simple text file. It doesn’t have a header or some other exceptional sections. It doesn’t even have and tags. It’s impartial unadulterated JavaScript code. The whole contents of a JavaScript file that grips the two functions in the instance above would be:

  1. function sayHi() {
  2. alert (“Hello world!”);
  3. li>
  4. function sayBye() {
  5. alert (“Buh-bye!”);
  6. li>

What creates a JavaScript file a JavaScript file is its file extension:.js.

The front end of the file name is up to us, on the condition that it’s legal. Some of these would be well.

scripts.js
coreJS.js
main-code.js
main_code.js
main.code.js

We take an explanation of a JavaScript file in an HTML file the same way we include an external CSS file—with an opening and closing tag.

It’s a good idea to place the markup that comprises JavaScript files at the end of the body section for the similar reason that it’s a good plan to write JavaScript code at the end of the body section. We may include as many external JavaScript files as you like. We can also contain JavaScript files and also embed other JavaScript code in the HTML.

Mansoor Ahmed is Chemical Engineer, web developer, a writer currently living in Pakistan. My interests range from technology to web development. I am also interested in programming, writing, and reading.
Posts created 422

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top