Where to write javascript code in html

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.



JavaScript makes HTML pages more dynamic and interactive.

The HTML Tag

The HTML tag is used to define a client-side script [JavaScript].

The element either contains script statements, or it points to an external script file through the src attribute.

Common uses for JavaScript are image manipulation, form validation, and dynamic changes of content.

To select an HTML element, JavaScript most often uses the document.getElementById[] method.

This JavaScript example writes "Hello JavaScript!" into an HTML element with id="demo":

Example


document.getElementById["demo"].innerHTML = "Hello JavaScript!";

Try it Yourself »

A Taste of JavaScript

Here are some examples of what JavaScript can do:

Example

JavaScript can change content:

document.getElementById["demo"].innerHTML = "Hello JavaScript!";

Try it Yourself »

Example

JavaScript can change styles:

document.getElementById["demo"].style.fontSize = "25px";
document.getElementById["demo"].style.color = "red";
document.getElementById["demo"].style.backgroundColor = "yellow";

Try it Yourself »

Example

JavaScript can change attributes:

document.getElementById["image"].src = "picture.gif";

Try it Yourself »

The HTML Tag

The HTML tag defines an alternate content to be displayed to users that have disabled scripts in their browser or have a browser that doesn't support scripts:

Example


document.getElementById["demo"].innerHTML = "Hello JavaScript!";

Sorry, your browser does not support JavaScript!

Try it Yourself »

HTML Exercises

Test Yourself With Exercises

Exercise:

Use JavaScript to change the HTML content of the

element to "Hello World!".

Hi.


document.["demo"].innerHTML = "Hello World!";

Start the Exercise

HTML Script Tags

TagDescription
Defines a client-side script
Defines an alternate content for users that do not support client-side scripts

For a complete list of all available HTML tags, visit our HTML Tag Reference.



Where is JavaScript code placed in HTML?

In HTML, JavaScript code is inserted between and tags.

How can I write HTML code in JavaScript?

HTML DOM Document write[].
function myFunction[] { document..
write["Hello World!" ]; }.

How do I connect JavaScript to 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ủ Đề