Home Web Front-end JS Tutorial There are several ways to use javascript code

There are several ways to use javascript code

Jun 10, 2021 am 11:10 AM
javascript

There are two ways to use JavaScript code, namely: 1. Direct execution. When a web page is opened, all JavaScript codes defined in script tags or linked js files will be executed; 2. Events Driver, when a certain event occurs, execute a certain piece of JavaScript code.

There are several ways to use javascript code

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

[JavaScript execution method]

The JavaScript code defined in the HTML document has two execution methods: direct execution and event-driven.

Direct execution:

When we open a web page, all JavaScript code defined in the <script> tag or linked js file will will be executed. </p> <p>It should be noted that functions defined with function are not executed. The function will only be executed when it encounters a function call. </p> <p>Example 1:</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>&lt;script type=&quot;text/javascript&quot;&gt; var d = new Date(); var m = d.getMonth(); if( m&gt;=5 ) document.write( m ); &lt;/script&gt;</pre><div class="contentsignin">Copy after login</div></div><p>The above JavaScript code is placed naked in the <script> tag, and will be executed immediately when the web page is opened. </p><p>Example 2: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>&lt;script type=&quot;text/javascript&quot;&gt; function getM() { var d = new Date(); var m = d.getMonth(); if( m&gt;=5 ) document.write( m ); } &lt;/script&gt; 以上JavaScript代码定义的是函数,当网页打开时,函数中的代码不会被执行。当需要执行该函数时,需要使用函数调用。 &lt;script type=&quot;text/javascript&quot;&gt; getM(); &lt;/script&gt;</pre><div class="contentsignin">Copy after login</div></div><p><code>Event-driven: </code></p><p>When an event occurs, such as a web page being opened, a mouse click, a double-click, etc., Execute a piece of JavaScript code. </p><p>For example: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>&lt;script type=&quot;text/javascript&quot;&gt; var count = 0; function setCount() { count++; if( count&gt;=5 ) count = 0; } &lt;/script&gt; &lt;button onclick=&quot;setCount()&quot;&gt;计数&lt;/button&gt;</pre><div class="contentsignin">Copy after login</div></div><p>In this example, the <button> tag defines a button, and the onclick attribute is used to respond to a mouse click event. When the button is clicked with the mouse , execute the setCount() function. </p><p>The attributes that respond to an event in the tag are called event handlers, and their values ​​are JavaScript codes. </p><p><span style="font-size: 16px;"><strong>Common event handles: </strong></span></p><p><strong>onload: </strong></p><p>Triggered when the web page is opened. It is only valid within the <body> and <frameset> tags. </p><p>This event is generally used to perform some initialization operations. </p><p><strong>onunload: </strong></p><p>Triggered when the web page is closed. It is only valid within the <body> and <frameset> tags. </p><p>This event is generally used to complete some finishing work. </p><p><strong>onclick: </strong></p><p>Triggered when the mouse clicks. It can be used for objects such as controls, images, text, hyperlinks, etc. </p><p>This event is used to respond to mouse click operations and is the most commonly used event handler. </p><p><strong>ondblclick: </strong></p><p>Triggered when the mouse is double-clicked. It can be used for objects such as controls, images, text, hyperlinks, etc. </p><p>This event is used to respond to a mouse double-click operation. </p><p><strong>onchange: </strong></p><p>Triggered when the content changes. It can be used for objects such as text boxes, list boxes, etc. </p><p>This event is generally used to respond to the user's operation of modifying the content in the text box. </p><p>Description: When the user enters text into a text box, the onchange event will not be triggered. This event will only be triggered when the user clicks an area outside the text box after completing the input, causing the text box to lose focus. . </p><p><strong>onselect: </strong></p><p>Triggered when the content is selected. It can be used for objects such as text boxes, list boxes, etc. </p><p>This event is generally used to respond to operations such as the user selecting the content in the text box and changing the selected item in the list box. </p><p>The above are just a few of the most commonly used event handlers. </p><p>Note: The event handler is not a JavaScript code, but an HTML attribute, so it is not case-sensitive, but you should develop the habit of writing in lowercase letters. </p><p><strong>Extended information: </strong></p><p>[JavaScript definition method]:</p><p>There are two ways to add JavaScript code to HTML documents: embedded and linked. Mode. </p><p>Embedded: </p><p>Embed JavaScript code in the HTML document. Method: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>&lt;script type=&quot;text/javascript&quot;&gt; JS代码 &lt;/script&gt;</pre><div class="contentsignin">Copy after login</div></div><p>JavaScript code must be defined between <script> and </script>.

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data

See all articles