<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> title </title> <script type="text/javascript"> function f() { var sHtml = document.body.innerHTML; document.body.innerHTML = sHtml; evalScript(sHtml); }
function getScript(str) { var matchAll = new RegExp('(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)', 'img'); var matchOne = new RegExp('(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)', 'im'); var a = str.match(matchAll) || []; var result = []; for(var i = 0; i <a.length; i++){ result.push((a[i].match(matchOne) || ['', ''])[1]); } return result; } function evalScript(str){ var scripts = getScript(str); for(var i = 0; i < scripts.length; i++){ eval(scripts[i]); } } </script> </head> <body onload="f()"> <script type="text/javascript"> alert("hello"); </script> </body> </html>
To hansonboy(): 因为现在页面的js内容特别多,所以采用那种方法效率非常低,IE会有超过十秒的停止响应现象。
To dh20156(风之石): 用defer的方法也有问题,页面中的js脚本是别人写的,脚本内容不可控制,同时也write出来一些页面的内容。
Sets or retrieves the status of the script.SyntaxHTML <SCRIPT DEFER ... > Scripting SCRIPT.defer [ = bDefer ] Possible ValuesbDefer Boolean that specifies or receives one of the following values.false Default. Inline executable function is not deferred. true Inline executable function is deferred. The property is read/write. The property has a default value of false.Expressions can be used in place of the preceding value(s), as of Microsoft® Internet Explorer 5. For more information, see About Dynamic Properties.Res
<head>
<title>
title
</title>
<script type="text/javascript">
function f() {
var sHtml = document.body.innerHTML;
document.body.innerHTML = sHtml;
evalScript(sHtml);
}
function getScript(str)
{
var matchAll = new RegExp('(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)', 'img');
var matchOne = new RegExp('(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)', 'im'); var a = str.match(matchAll) || [];
var result = [];
for(var i = 0; i <a.length; i++){
result.push((a[i].match(matchOne) || ['', ''])[1]);
}
return result;
}
function evalScript(str){
var scripts = getScript(str);
for(var i = 0; i < scripts.length; i++){
eval(scripts[i]);
}
}
</script>
</head>
<body onload="f()">
<script type="text/javascript">
alert("hello");
</script>
</body>
</html>
因为现在页面的js内容特别多,所以采用那种方法效率非常低,IE会有超过十秒的停止响应现象。
用defer的方法也有问题,页面中的js脚本是别人写的,脚本内容不可控制,同时也write出来一些页面的内容。
Scripting SCRIPT.defer [ = bDefer ] Possible ValuesbDefer Boolean that specifies or receives one of the following values.false Default. Inline executable function is not deferred.
true Inline executable function is deferred.
The property is read/write. The property has a default value of false.Expressions can be used in place of the preceding value(s), as of Microsoft® Internet Explorer 5. For more information, see About Dynamic Properties.Res
<head>
<title>
title
</title>
<script type="text/javascript">
function f() {
var ct=0;
document.body.innerHTML.replace(/\<script[^>]+>([\s\S]+?)<\/script>/ig,function($1,$2){
eval($2);
});
}
</script>
</head>
<body onload="f()">
<script type="text/javascript">
alert("hello");
</script>
<script type="text/javascript">
alert("1");
</script>
<script type="text/javascript">
alert("2");
</script>
</body>
</html>