Alexis Eggermont
Alexis Eggermont

Reputation: 8145

Redirect based on country

I would like to host a webpage on Github, which does not support php, and then redirect all visitors, except those in China, to another website (hosted on Weebly).

The reason is that Weebly is blocked in China but provides a better user experience.

How can I do that without PhP enabled? Is there a way to do that in Javascript?

Upvotes: 0

Views: 12222

Answers (4)

storm is-in
storm is-in

Reputation: 19

<script>
function geo(a) {
switch (a.country.code)
{
   case "SE": // Redirect is visitor from Sweden
   case "NO": // Redirect is visitor from Norway
   case "FR": // Redirect is visitor from FRANCE
   case "PL": // Redirect is visitor from POLAND
   case "GY": // Redirect is visitor from GERMANY
       window.location = "https://se.brixtol.com" + window.location.pathname; // edit for your URL
       break;
   default: 
       return null;
}
}
</script>enter code here`
<script src="https://geoip.nekudo.com/api?callback=geo"></script>
</pre>

Upvotes: 0

storm is-in
storm is-in

Reputation: 19

document.write("Welcome to our visitors from "+geoplugin_countryName()); 
 <script language="JavaScript" src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>
 </head>

Upvotes: 1

kiks73
kiks73

Reputation: 3758

You can also use ipinfo.io

var xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', '//ipinfo.io', true);
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4) {
        if(xmlhttp.status == 200) {
            var obj = JSON.parse(xmlhttp.responseText);
            if (obj.country != 'CN')
                window.location.replace('http://www.weebly.com/...');
         }
    }
};
xmlhttp.send(null);

Upvotes: 1

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

You can use geoPlugin

First Add

<script language="JavaScript" src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>

to your webpage

then try this

document.write("Welcome to our visitors from "+geoplugin_countryName()); 
 <script language="JavaScript" src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>
 </head>

Upvotes: 5

Related Questions