javascript:Asynchronous Scripts,xhtml,named argument

本文解释了HTML5中<script>元素的async属性,它如何改变脚本处理方式,与defer属性的区别,并讨论了其在加载顺序上的不确定性及其对DOM的影响。同时,介绍了将JavaScript代码转换为XHTML有效版本的方法,包括使用CDATA段或JavaScript注释解决字符限制问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Asynchronous Scripts
HTML5 introduces the async attribute for <script> elements. The async attribute is similar to defer
in that it changes the way the script is processed. Also similar to defer, async applies only to external
scripts and signals the browser to begin downloading the file immediately. Unlike defer, scripts
marked as async are not guaranteed to execute in the order in which they are specifi ed.
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 fi le might execute before the fi rst, so it’s important that there are no
dependencies between the two. The purpose of specifying an async script 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
.


The second option for turning this code into a valid XHTML version is to wrap the JavaScript
code in a CDATA section. In XHTML (and XML), CDATA sections are used to indicate areas of
the document that contain free-form text not intended to be parsed. This enables you to use any
character, including the less-than symbol, without incurring a syntax error. The format is as follows:
<script type=”text/javascript”><![CDATA[
function compare(a, b) {
if (a < b) {
alert(“A is less than B”);
} else if (a > b) {
alert(“A is greater than B”);
} else {
alert(“A is equal to B”);
}
}
]]></script>

In XHTML-compliant web browsers, this solves the problem. However, many browsers are still
not XHTML-compliant and don’t support the CDATA section. To work around this, the CDATA
markup must be offset by JavaScript comments:
<script type=”text/javascript”>
//<![CDATA[
function compare(a, b) {
if (a < b) {
alert(“A is less than B”);
} else if (a > b) {
alert(“A is greater than B”);
} else {
alert(“A is equal to B”);
}
}
//]]>
</script>


Any named argument that is not passed into the function is automatically assigned the value
undefined. This is akin to defining a variable without initializing it. For example, if only one
argument is passed into the doAdd() function, then num2 has a value of undefined.
Strict mode makes several changes to how the arguments object can be used. First, assignment,
as in the previous example, no longer works. The value of num2 remains undefined even though
arguments[1] has been assigned to 10. Second, trying to overwrite the value of arguments is a
syntax error. (The code will not execute.)


If two functions are defi ned to have the same name in ECMAScript, it is the last function that
becomes the owner of that name.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值