1

I am using this code in php or html file for redirect

<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>

but i want to use a js file which do the same function but its extension should be .js

as http://domain.com/redirect.js so what would be the code?

1 Answer 1

1

This includes a script include function with a callback, so that the Maxmind client code won't run until the Maxmind library has been loaded.

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

The best way to let me know is to upvote and choose mine as the correct answer. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.