我在php或html文件中使用此代码进行重定向
<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>
<script language="JavaScript">
var country= geoip_country_code();
if(country == "US" )
{
<!--
window.location = "http://google.com"
//-->
}
</script>
但我想使用一个js文件,该文件执行相同的功能,但其扩展名应为.js
为http://domain.com/redirect.js那么代码是什么?
答案 0 :(得分:1)
这包括带回调的脚本包含函数,以便Maxmind客户端代码在加载Maxmind库之前不会运行。
loadScript("http://j.maxmind.com/app/geoip.js", function() {
var country = geoip_country_code();
if (country === "US") {
window.location = "http://google.com/";
}
});
function loadScript(url, callback) {
// adding the script tag to the head as suggested before
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
// then bind the event to the callback function
// there are several events for cross browser compatibility
script.onreadystatechange = callback;
script.onload = callback;
// fire the loading
head.appendChild(script);
}