代碼:111.js
document.write("<script language='javascript' src='http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=222.5.142.25'></script>");
document.write("<script>var gj=remote_ip_info.country;</script>")
var Url,kk,cHeight,cWidth,cReferrer,cPage,gj;
Url="http://127.0.0.1:82/tj/cs_tjcs.asp";
kk="123"
//gj=remote_ip_info.country;
cHeight = screen.height;
document.write("<script language=javascript src="+Url+"?kp="+kk+"&gj="+gj+"></script>");
此代碼中,直接通過var gj=...無法獲取值,通過document.write("var gj=remote_ip_info.country;")可以獲取值,但是在最後一行代碼調用時確無法取到值&gj="+gj+"。
通過document.write("var gj=remote_ip_info.country;")獲取值後,可以通過document.write("alert(gj);")正確顯示。
要求在最後一行代碼中取到值,在線等大俠指教,謝謝!
參考document.write, onload and variable scope issue
直接用document.write不能保證立即獲得新浪的返回值,可能就會出現取不到值的問題。可以在script的onload時再調用後續代碼
<script type="text/javascript">
var myLoader = function(filename, fun) {
var head = document.getElementsByTagName('head')[0],
script = document.createElement('script');
script.type= 'text/javascript';
script.src = filename;
if(typeof fun === 'function'){ script.onload = fun; }
head.appendChild(script);
};
myLoader("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=222.5.142.25", function(){
var gj=remote_ip_info.country;
var Url,kk,cHeight,cWidth,cReferrer,cPage,gj;
Url="http://127.0.0.1:82/tj/cs_tjcs.asp";
kk="123"
//gj=remote_ip_info.country;
cHeight = screen.height;
console.log(gj);
myLoader(Url+"?kp="+kk+"&gj="+gj, function(){});
});
</script>