我刚看到这个示例代码,它有一个带有外部源和主体的脚本标记.我假设这是一种将一些信息传递给包含脚本的聪明方法.它是如何工作的?
<html>
<head>
<script src="http://unhosted.org/remoteStorage.js">{
onChange: function(key, oldValue, newValue) {
if(key=='text') {
document.getElementById('textfield').value= newValue;
}
},
category: 'documents'
}</script>
</head>
Run Code Online (Sandbox Code Playgroud)
在正常情况下,不会评估标记中包含的脚本.我认为在你的例子中发生的remoteStorage.js是在评估时读取内容本身.这样的事情
//grab the last script tag in the DOM
//this will always be the one that is currently evaluating during load
var tags = document.getElementsByTagName('script');
var tag = tags[tags.length -1];
//force evaluation of the contents
eval( tag.innerHTML );
Run Code Online (Sandbox Code Playgroud)
虽然这在标记中看起来很整洁,但我自己只会使用单独的脚本标记并避免这种强制评估.
| 归档时间: |
|
| 查看次数: |
697 次 |
| 最近记录: |