我有两个文件(html 和 js),如下所示。
测试.html
<body>
<script>
/*Loads the fb api*/
window.fbAsyncInit = function() {
FB.init({
appId : '<APP-ID>',
status : true,
cookie : true,
xfbml : true,
oauth : true,
});
TimelineScript(); /* function that loads the external javascript */
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
. . .
. . .
. . .
function TimelineScript(){
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
var dt = new Date();
var _testvar = 100;/*Used in the external javascript file*/
/*Loading my external javscript*/
(function() {
var sa = document.createElement('script');
sa.type = 'text/javascript';
sa.async = true;
sa.src = 'http://www.mywebsite.com/javascripts/testing.js?random='+dt.getTime();
var script_tag = document.getElementsByTagName('script')[0];
script_tag.parentNode.insertBefore(sa, script_tag);
})();
} else if (response.status === 'not_authorized') {
alert("the user is logged in to Facebook, but not auth your app");
} else {
alert('user is not logged into fb!');
}
});
}
</script>
. . .
. . .
. . .
</body>
测试.js
alert('loaded testing .js'); //works
alert(_testvar); //throws error
_testvar is not defined
testing.js 文件中的警报在firebug 控制台中引发错误。如果我有一个仅声明一个变量然后加载外部 js 的纯脚本(没有所有 facebook api 代码),则警报工作正常。在这种情况下出了什么问题?