Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Introduction to javascript


May 06, 2021 JavaScript


Table of contents


Introduction to JavaScript


JavaScript is the most popular scripting language on the Internet, which can be used for HTML and the web, and more widely for devices such as servers, PCs, laptops, tablets, and smartphones.


JavaScript is the scripting language

JavaScript is a lightweight programming language.

JavaScript is programmable code that can be inserted into HTML pages.

JavaScript can be executed by all modern browsers when it is inserted into an HTML page.

JavaScript is easy to learn.


What you will learn

Here are the main steps you'll learn in this tutorial.


JavaScript: Write directly to the HTML output stream

document.write (""-lt;h1-gt; this is a title-lt;/h1-gt;");
document.write ("lt;p-gt; this is a paragraph. </p>");

Try it out . . .

Introduction to javascript You can only use document.write in HTML output. If you use this method after the document is loaded, the entire document is overwritten.


JavaScript: Reaction to events

<button type="button" onclick="alert('欢迎!')">点我!</button>

Try it out . . .

The alert() function is not commonly used in JavaScript, but it is convenient for code testing.

The onclick event is just one of many events you'll learn about in this tutorial.


JavaScript: Change HTML content

Using JavaScript to handle HTML content is a very powerful feature.

X = Document.GtelementByid ("demo") // Find elements
x.innerhtml = "Hello JavaScript"; // Change the content

Try it out . . .

You'll often see document.getElementById ("some id"). ") This method is defined in HTML DOM.

DOM (D ocument O bject M odel) (document object model) is the official W3C standard for accessing HTML elements.

You'll learn about HTML DOM in several chapters of this tutorial.


JavaScript: Change HTML images

This example dynamically changes the source (src) of html images:

Light up the bulb

Introduction to javascript

Click on the bulb to open or close this light


Try it out . . .


The "light bulb" implementation code is detailed:

<script>
function changeImage()
{
    element=document.getElementById('myimage')
    if (element.src.match("bulbon"))
    {
        element.src="/images/pic_bulboff.gif";
    }
    else
    {
        element.src="/images/pic_bulbon.gif";
    }
}
</script>
<img id="myimage" onclick="changeImage()" src="/images/pic_bulboff.gif" width="100" height="180">

In the above example, the code element.src.match ("bulbon") means:

Retrieving the value of the src property contains the bulbon string, if there is a string, the picture src is updated to "changeImage()" src?/images/pic_bulboff.gif"width""100" bulb" height""180" and the value of the src property inside the bulbon, if there is a string, the picture src is updated to .gif, and if the string is not matched, the src is updated to bulbon.gif


JavaScript can change most properties of any HTML element, not just pictures.


JavaScript: Change the HTML style

Changing the style of an HTML element is a variant of changing HTML properties.

x = Document.GetElementByid ("demo") // Find element
x.Style.Color = "# ff0000"; // Change the style

Try it out . . .


JavaScript: Verify the input

JavaScript is often used to validate the user's input.

If isnan (x) {alert ("not a number)};

Try it out . . .

The above examples are just ordinary validations, and if you want to use them in a production environment, you need to be strictly judged if you enter a space, or if a continuous space isNaN is not judged.

Did you know that?

Introduction to javascript JavaScript and Java are two completely different languages, both conceptually and by design.
Java (invented by Sun) is a more complex programming language.

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

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

The relationship between javaScript and ECMAScript

ECMAScript is a scripting language standardized by the European Association of Computer Manufacturers through ECMA-262.

JavaScript has been standardized by ECMA through ECMAScript.


Related tutorials

ECMAScript tutorial