[JavaScript tutorial] JavaScript usage

Source: Internet
Author: User
[JavaScript tutorial] JavaScript usage

JavaScript usage

The script in HTML must be located between the script tag and the script tag.

Scripts that can be placed on HTML pagesAnd.

Script tag

To insert JavaScript into the HTML page, use the script tag.

Script and script tell JavaScript where to start and end.

The code lines between script and script contain JavaScript:

Script alert ("My first JavaScript"); script

You do not need to understand the above Code. You only need to understand that the browser will interpret and execute the JavaScript code between script and script.

Old instances may use type = "text/javascript" in the script tag ". You don't have to do this now. JavaScript is the default scripting language in all modern browsers and HTML5.

In JavaScript

In this example, JavaScript loadsWrite text:

Instance

.. Script document. write ("this is a title"); document. write ("

This is a paragraph

"); Script...

JavaScript Functions and events

The JavaScript statement in the preceding example is executed during page loading.

Generally, we need to execute code when an event occurs, for example, when a user clicks a button.

If we put JavaScript code into a function, we can call this function when an event occurs.

You will learn more about JavaScript Functions and events later.

InOrJavaScript

You can add an unlimited number of scripts in the HTML document.

Scripts can be located in HTMLOrOr both.

The common practice is to put the functionOr at the bottom of the page. In this way, you can place them in the same location without interfering with the page content.

JavaScript Functions in

In this example, we place a JavaScript function in.

This function will be called when you click the button:

Instance

Script function myFunction () {document. getElementById ("demo"). innerHTML = "My first JavaScript function";} scriptMy Web page

One paragraph

Try it

JavaScript Functions in

In this example, we place a JavaScript function in.

This function will be called when you click the button:

Instance

My Web page

One paragraph

Try itScript function myFunction () {document. getElementById ("demo"). innerHTML = "My first JavaScript function";} script

External JavaScript

You can also save the script to an external file. An external file usually contains code used by multiple webpages.

The extension of an external JavaScript file is. js.

To use an external file, set the. js file in the "src" attribute of the script Tag:

Instance

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.