0

I am using code below for for my js file for redirecting.. but i want my redirection based on top location. for example if someone visit xyz.com so it redirect to mydomain.com so what code i needed to add? i think it could be like indexOf('xyz.com')

loadScript("http://j.maxmind.com/app/geoip.js", function() {
    var country = geoip_country_code();

    if (country === "US") {
        window.location = "http://mydomain.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);
}
4

1 回答 1

0

I am not totally sure I understand your question,

window.location is an object which refers to the currently displayed web page at all times. As you already know you can change window.location and cause a page change in the browser.

window.location also has several properties which help in analyzing its contents, see the referenced link for more details. window.location.host is the one you need I think.

So if I understand your question correctly, you could replace

var country = geoip_country_code();

if (country === "US") {
    window.location = "http://mydomain.com/";
}

with

if (window.location.host === 'xyz.com') {
    window.location = "http://mydomain.com/";
}
于 2012-05-26T01:03:18.360 回答