Javascript Program Execution

Source: Internet
Author: User

① Execute the script

Javascript statements that appear between <SCRIPT> and </SCRIPT> are executed in the order they appear in the script. When a file has multiple scripts, the scripts are executed in the order they appear. The script can appear in .

② Onload event handle

You can set the onload attribute of the <body> tag to register an onload handle. When an onload handle is triggered, the document is fully loaded and parsed, and any document element can be operated by JavaScript code. Therefore, the Javascript module that modifies the content of the document usually contains a function that executes the modification, and the event registration code that is responsible for arranging function calls when the document is fully loaded. Because onload event handles are called after the document is completely parsed, they cannot call document. write (). any such call re-opens a new document and overwrites the current document, instead of adding content to the end of the current document. The user does not even have the opportunity to see the current document.

③ OnUnload Event handle

When the user leaves a web page in the navigation bar, the browser triggers the onUnload Event handle to give the JavaScript code on the page the last chance to run. You can set the onUnload attribute of the <body> tag to define an onUnload handle. The onUnload handle should not run any time-consuming operations, nor should it pop up a dialog box. Its exit is just a quick clean-up operation. Running it should not slow down or prevent users from switching to a new page.

④ Window object used as the execution environment

All scripts, event handles, and JavaScript URLs in this document share the same window object as their global objects. Javascript variables and functions are just attributes of this global object.

Since the onload event is not called before all scripts are executed, each onload event handle can access all functions defined by all scripts in the document and all declared variables.

At any time, when a new object is loaded into a window, the window object of the window is restored to its default state: all attributes and functions defined by the script in the previous document are deleted and any standard system attributes that may have been modified or overwritten are restored. Each document starts with an innocent history.

⑤ Client JavaScript thread model

The core JavaScript language does not contain any thread mechanism, and the client JavaScript does not add any thread mechanism. Client Javascript is single-threaded. When the script is loaded and executed, the File Parsing is stopped, and the Web browser stops responding to user input when the event handle is executed.

Single-threaded execution is made for simpler scripts. You can write code to ensure that the two event handles do not run simultaneously. You can operate on the document content and know in advance that no other thread tries to modify the document at the same time.

Single-threaded execution can also burden JavaScript programmers, which means that JavaScript scripts and event handles cannot run for too long. If a script executes computation-intensive tasks, it will delay document loading, and users cannot see the document content before the script is complete. If an event handle executes computation-intensive tasks, the browser may become unable to respond, and the user may think that the browser has crashed.

If the application must execute enough computing to cause a significant delay, the file should be allowed to fully load before the computing is executed. Ensure that the user computing is being notified and the browser is not suspended. If the calculation may be divided into discrete subtasks, you can run the subtasks in the background using methods such as setTimeout () and setinterval (), and update a progress indicator to display feedback to the user.

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.