以下片段摘自an example in Google API doc.这段代码中有趣的部分是,<head>
中的两个<script async>
中的onload
事件处理程序在后面的<body>
中定义.异步脚本中的onload
事件是否只有在解析<body>
之后才会触发?是否有任何规范提供此类保证?或者这段代码只有在隐含的假设下才是正确的,即<head>
中的这两个特定脚本需要很长时间来获取和执行?
<!DOCTYPE html>
<html>
<head>
<script async defer src="https://apis.google.com/js/api.js" onload="gapiLoad()"></script>
<script async defer src="https://accounts.google.com/gsi/client" onload="gisInit()"></script>
</head>
<body>
<script>
function gapiLoad() {
// do something
}
function gisInit() {
// do something
}
// there are other stuffs...
</script>
</body>
</html>