Html script to js file

❮ HTML tag

Example

Point to an external JavaScript file:

Try it Yourself »

Definition and Usage

The src attribute specifies the URL of an external script file.

If you want to run the same JavaScript on several pages in a web site, you should create an external JavaScript file, instead of writing the same script over and over again. Save the script file with a .js extension, and then refer to it using the src attribute in the tag.

Note: The external script file cannot contain the tag.

Note: Point to the external script file exactly where you would have written the script.

Browser Support

Attribute
src Yes Yes Yes Yes Yes

Syntax

Attribute Values

ValueDescription
URL The URL of the external script file.

Possible values:

  • An absolute URL - points to another web site [like src="//www.example.com/example.js"]
  • A relative URL - points to a file within a web site [like src="/scripts/example.js"]

❮ HTML tag


The Tag

In HTML, JavaScript code is inserted between and tags.

Example


document.getElementById["demo"].innerHTML = "My First JavaScript";

Try it Yourself »

Old JavaScript examples may use a type attribute: .
The type attribute is not required. JavaScript is the default scripting language in HTML.

JavaScript Functions and Events

A JavaScript function is a block of JavaScript code, that can be executed when "called" for.

For example, a function can be called when an event occurs, like when the user clicks a button.

You will learn much more about functions and events in later chapters.

JavaScript in or

You can place any number of scripts in an HTML document.

Scripts can be placed in the , or in the section of an HTML page, or in both.

JavaScript in

In this example, a JavaScript function is placed in the section of an HTML page.

The function is invoked [called] when a button is clicked:

Example





function myFunction[] {
  document.getElementById["demo"].innerHTML = "Paragraph changed.";
}


Demo JavaScript in Head

A Paragraph


Try it


Try it Yourself »

JavaScript in

In this example, a JavaScript function is placed in the section of an HTML page.

The function is invoked [called] when a button is clicked:

Example



Demo JavaScript in Body

A Paragraph

Try it


function myFunction[] {
  document.getElementById["demo"].innerHTML = "Paragraph changed.";
}


Try it Yourself »

Placing scripts at the bottom of the element improves the display speed, because script interpretation slows down the display.

External JavaScript

Scripts can also be placed in external files:

External file: myScript.js

function myFunction[] {
  document.getElementById["demo"].innerHTML = "Paragraph changed.";
}

External scripts are practical when the same code is used in many different web pages.

JavaScript files have the file extension .js.

To use an external script, put the name of the script file in the src [source] attribute of a tag:

You can place an external script reference in or as you like.

The script will behave as if it was located exactly where the tag is located.

External scripts cannot contain tags.

External JavaScript Advantages

Placing scripts in external files has some advantages:

  • It separates HTML and code
  • It makes HTML and JavaScript easier to read and maintain
  • Cached JavaScript files can speed up page loads

To add several script files to one page  - use several script tags:

Example


External References

An external script can be referenced in 3 different ways:

  • With a full URL [a full web address]
  • With a file path [like /js/]
  • Without any path

This example uses a full URL to link to myScript.js:

Example

Try it Yourself »

This example uses a file path to link to myScript.js:

This example uses no path to link to myScript.js:

You can read more about file paths in the chapter HTML File Paths.



Can we write HTML code in JS file?

JavaScript in or You can place any number of scripts in an HTML document.

How do I connect JavaScript and HTML?

Adding JavaScript into an HTML Document You can add JavaScript code in an HTML document by employing the dedicated HTML tag that wraps around JavaScript code. The tag can be placed in the section of your HTML or in the section, depending on when you want the JavaScript to load.

What is script src in HTML?

The src attribute specifies the URL of an external script file. If you want to run the same JavaScript on several pages in a web site, you should create an external JavaScript file, instead of writing the same script over and over again. Save the script file with a .

Where do you link scripts in HTML?

We can link JavaScript to HTML by adding all the JavaScript code inside the HTML file. We achieve this using the script tag which was explained earlier. We can put the tag either inside the head of the HTML or at the end of the body.

Chủ Đề