Thứ Tư, 21 tháng 8, 2013

Asynchronous Scripts

HTML5 introduces the asyncattribute for <script>elements. The asyncattribute is similar to defer
in that it changes the way the script is processed.
For example:
<!DOCTYPE html>
<html>
<head>
<title>Example HTML Page</title>
<script type=”text/javascript” async src=”example1.js”></script>
<script type=”text/javascript” async src=”example2.js”></script>
</head>
<body>
<!-- content here -->
</body>
</html>
In this code, the second script file might execute before the first, so it’s important that there are no
dependencies between the two. The purpose of specifying an asyncscript is to indicate that the
page need not wait for the script to be downloaded and executed before continuing to load, and it
also need not wait for another script to load and execute before it can do the same. Because of this,
it’s recommended that asynchronous scripts not modify the DOM as they are loading.
Asynchronous scripts are guaranteed to execute before the page’s loadevent and may execute
before or after DOMContentLoaded(see Chapter 13 for details). Firefox 3.6, Safari 5, and Chrome 7
support asynchronous scripts.

Không có nhận xét nào:

Đăng nhận xét