Redirect Java Script or HTML [closed]

-4

I'd like to be doing the same redirection based on the user's location. Do you have this possibility?

ex: If he is in Brazil he stays in the site. if it is from another country it is automatically redirected to another site.

Thank you

    
asked by anonymous 06.11.2018 / 19:02

2 answers

1

There is this possibility Bernardo :). You can use some service that already provided this information like these for example:

For these 4, you need to pay a fee for consultations depending on the plan. A free alternative is ip-api . To test your location, go to the url below in your browser:

http://ip-api.com/json

On your site, you'll need this code snippet well before the body tag closes:

 $(document).ready(function(){
     $.get({
         url:'http://ip-api.com/json', 
         timeout: 5000
     }).then(
         //Funcao de sucesso
         function success(response) {
            if(!response.country === "Brazil") {
                //Caso sim, redireciona para outra url
                window.location.href = "https://www.nytimes.com/";
             }
         }, 
         //Funcao de erro
         function fail(data, status) {
            //Continua no site em portugues
         }
      );
 });

You'll need to have imported jQuery . Remembering that as it is a free service, you can suffer intermittency / slowness for calls. Details: Country names are returned in English.

    
07.11.2018 / 01:31
0

Bernardo, the only modifications needed in this code, is the URL that you should use and the parameter that returns the name of the country. The url should follow the following pattern: https://api.ipgeolocation.io/ipgeo?apiKey=API_KEY , where API_KEY should be replaced with the value that is on your tool dashboard in the Your API Key section. The field that returns the country name changes to country_name . This way, the above code for ipgeolocation is:

<script type="text/javascript">
    $(document).ready(function(){
        $.get({
            url:'https://api.ipgeolocation.io/ipgeo?apiKey=1234567890', 
            timeout: 5000
        }).then(
            //Funcao de sucesso
            function success(response) {
                if(!response.country_name === "Brazil") {
                    //Caso sim, redireciona para outra url
                    window.location.href = "https://www.nytimes.com/";
                }
            }, 
            //Funcao de erro
            function fail(data, status) {
                //Continua no site em portugues
            }
        );
    });
</script>'
    
07.11.2018 / 12:47