从 Js 文件重定向


Redirect From a Js File

>我在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 代码是什么?

这包括

一个带有回调的脚本包含函数,因此在加载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);
}