Website development | JavaScript tutorial

Source: Internet
Author: User
JavaScript is the most popular programming language in the world. This language can be used for HTML and web, and is more widely used in servers, PCs, laptops, tablets, smartphones, and other devices. JavaScript is the most popular programming language in the world. This language can be used for HTML and web, and is more widely used in servers, PCs, laptops, tablets, smartphones, and other devices.

JavaScript is a scripting language

JavaScript is a lightweight programming language.

JavaScript is the programming code that can insert HTML pages.

After JavaScript is inserted into the HTML page, it can be executed by all modern browsers.

JavaScript: write HTML output

Instance

document.write("This is a heading");document.write("

This is a paragraph

");

Tip: You can only use document. write in HTML output. If you use this method after the document is loaded, the entire document will be overwritten.

JavaScript: responds to events

Instance

Click here

Alert () functions are not commonly used in JavaScript, but they are very convenient for code testing.

Onclick events are only one of the many events you will learn in this tutorial.

JavaScript: modifying HTML content

Using JavaScript to process HTML content is very powerful.

Instance

X = document. getElementById ("demo") // search for the element x. innerHTML = "Hello JavaScript"; // change the content

You will often see document. getElementByID ("some id "). This method is defined in html dom. DOM (Document Object Model) is a formal W3C standard used to access HTML elements.

JavaScript: Change the HTML Style

Modifying the style of HTML elements is a variant of modifying HTML attributes.

Instance

X = document. getElementById ("demo") // find the x. style. color = "# ff0000"; // change the style.

JavaScript: Verify Input

JavaScript is often used to verify user input.

Instance

if isNaN(x) {alert("Not Numeric")};

Do you know?

Prompt: JavaScript and Java are two completely different languages, both in terms of concept and design.

Java (invented by Sun) is a more complex programming language.

The ECMA-262 is the official name of the JavaScript standard.

JavaScript was invented by Brendan Eich. It appeared in Netscape in 1995 (the browser has stopped updating) and was adopted by ECMA in 1997.

Use JavaScript

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 JavaScript 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 heading");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.


OrIn JavaScript

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";}《script》My Web Page

A 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

A Paragraph

Try it《script》function myFunction(){document.getElementById("demo").innerHTML="My First JavaScript Function";}《script》

Prompt: We put JavaScript at the bottom of the page Code to ensure that

The script is executed after the element is created.

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

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.