Home > Article > Web Front-end > What is javascript script interpreted and executed by?
Javascript scripts are interpreted and executed by the browser. The situations in which javascript scripts are executed: 1. The browser executes the script when the page is opened; 2. Uses the onLoad event to execute the script; 3. Uses user events to execute the script.
The operating environment of this article: Windows 7 system, javascript version 1.8.5, DELL G3 computer
How is the javascript script interpreted and executed?
Javascript scripts are interpreted and executed by the browser.
When will the javascript script be executed?
Javascript scripts can be embedded anywhere within HTML, but when is it called? When the 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. These situations are analyzed below.
1. The browser executes the script when opening the page
When the browser opens an HTML file, it will interpret 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 encountered. There are mainly the following two situations:
1). When the program starts (here refers to the browser loading page), functions such as alert will be triggered to execute
<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 The loading and parsing js function is automatically called (not triggered by user clicks and other behaviors)
2. Use the onLoad event to execute the script (equivalent to listening for ** to occur and then executing it)
The onLoad event is a Occurs when the page is opened in the browser. This method is often used to display some messages to the user while opening a page.
The following example uses the onLoad event of the tag for demonstration:
<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. Use user events to execute scripts
Users often use the mouse and keyboard to perform some operations when using the browser Operations, such as moving the mouse proportionally, clicking links or buttons, these operations will generate corresponding events. We can use these events to call script functions.
The following example uses a click button event to call the display() function.
<html> <head> <title>demo</title> <script type="text/javascript"> //insert javascript code here. function display(){ alert("you click me ,it is so pain") } </script> </head> <body> <center><br> <form> <input type="button" value="onclick" onclick="display()"> </form> </center> </body> </html>
Recommended study: "js Basic Tutorial"
The above is the detailed content of What is javascript script interpreted and executed by?. For more information, please follow other related articles on the PHP Chinese website!