运行 play framework 2.3 版,虽然它可能不相关:
我有一个包含以下内容的 html 文件:
<html>
<head>
<script type="text/javascript"> if (typeof x === 'undefined') {console.log("x not defined");} else {console.log("in html, x is %s", typeof x);} </script>
<script type="text/javascript" src="javascripts/somescript.js"></script>
</head>
</html>
somescript.js 有这个:
(function() {
jQuery(function() {
if (typeof x === 'undefined') {
console.log("in somescript.js, x is %s", typeof x);
var x = something;
//other stuff
}
});
}).call(this);
当我第一次加载页面时, x 按预期未定义。但是,当我转到同一应用程序中的不同页面,然后返回时,控制台显示:
in html, x is object
in somescript.js, x is undefined
这很奇怪,因为在 html 中,if 语句为 false,但在 somescript.js 中,相同的 if 语句为 true。
为什么要这样做,我如何确保两个脚本以相同的方式运行?