0

私はリダイレクトのために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として、コードはどうなるでしょうか。

4

1 に答える 1

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);
}
于 2012-04-24T22:50:50.057 に答える