JavaScript scripts can be embedded anywhere in HTML, but when is it called? When a browser opens an HTML file, it will directly run a script that is not a declared function or call a script function through an event. The following analyzes these situations. 1. The browser executes the script when opening the pageWhen a browser opens an HTML file, it interprets the entire file from scratch, including HTML tags and scripts. If there are statements in the script that can be executed directly, they will be interpreted and executed immediately when they are encountered. There are mainly two situations: 1). When the program starts (here the browser loads the page), this alert function will be triggered and executed.<html> <head> <title>demo</title> <script type="text/javascript"> alert("dare you click me once again"); </script> </head> <body onLoad="display()"> </body> </html> 2). As the browser loads and parses the js function, it is automatically called (not triggered by user clicks, etc.) 2. Use the onLoad event to execute the script (equivalent to listening to the occurrence of ** and then executing) The onLoad event occurs when a page is opened in a browser. This method is often used to display some messages to the user while opening a page. <html> <head> <title>demo</title> <script type="text/javascript"> //insert javascript code here. function display() { alert("dare you click me once again") } </script> </head> <body onLoad="display()"> </body> </html> 3. Execute scripts using user events When using a browser, users often use the mouse and keyboard to perform some operations, such as moving the mouse proportionally, clicking links or buttons, and these operations will generate corresponding events. We can use these events to call script functions. <html> <head> <title>demo</title> <script type="text/javascript"> //insert javascript code here. function display(){ alert("you click me ,it is so painful") } </script> </head> <body> <center><br> <form> <input type="button" value="onclick" onclick="display()"> </form> </center> </body> </html> The above is a detailed explanation of when JavaScript scripts will be executed. For more information about when JavaScript scripts will be executed, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: MySQL Optimization: InnoDB Optimization
>>: 27 Linux document editing commands worth collecting
Web forms are the primary communication channel b...
Recently I have been saying that design needs to h...
This article mainly explains how to deploy Elasti...
principle The principle of anti-shake is: you can...
This article example shares the specific code of ...
Table of contents definition Constructor bodies a...
introduction: There are a lot of information and ...
Table of contents 1. Retrieve via --skip-grant-ta...
This is a test of the interviewee's basic kno...
When designing table structures, numeric types ar...
1. Conventional writing in vue2 // The parent com...
Table of contents Get the time in the past week G...
Question 1: How do you instruct the browser to dis...
What is HTML? To put it simply: HTML is used to m...
Data backup and restore part 3, details are as fo...