1
如this SO question和其他许多类似规定中所述,页面上执行<script>
的顺序应与html文档中定义这些标签的顺序相同。JavaScript - 执行顺序<script>标签
我创建了一个简单的Java(服务器端)的测试应用程序,允许执行的请求,并且(在这个问题的底部有关的代码片段)返回响应之前等待指定时间内。它有一个简单的API:
http://localhost:8080/latency?time=XXX&response=YYY
实例的请求,将在一秒钟后返回console.log('done')
(1000毫秒):
http://localhost:8080/latency?time=1000&response=console.log(%27done%27)
接下来,我创建了一个简单的index.html页(nginx的服务):
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test order</title>
</head>
<body>
<script type="text/javascript" async=false src="http://localhost:8080/latency?time=1000&response=console.log(%27done1%27)"></script>
<script type="text/javascript" async=false src="http://localhost:8080/latency?time=100&response=console.log(%27done2%27)"></script>
<script type="text/javascript" async=false src="http://localhost:8080/latency?time=10&response=console.log(%27done3%27)"></script>
<script>console.log('static script without "src" attr');</script>
</body>
</html>
,根据我看到的一切使我至今预计控制台输出的顺序是:
done1
done2
done3
static script without "src" attr
这是我得到了(火狐51开发者控制台):
这只是我期望得到相反的顺序。我错过了什么吗? 是否有一种方法可以按照所需的顺序执行这些脚本(即按照它们在HTML中定义的顺序)?
作为参考,在服务器侧的爪哇部分:
private String latency(HttpServletRequest request) {
long millis = Long.parseLong(request.getParameter("time"));
String response = request.getParameter("response");
try {
Thread.sleep(millis);
return (response != null) ? response : "";
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
'异步=“假”'(带引号),有何帮助? – haxxxton
或者完全删除'async' –
'async =“false”'使它异步。删除这些属性。 – nicovank