Infinite loop gives pagescaping window.ready or window.onload

Asked

Viewed 42 times

-2

I’m trying to load a page by satisfying a condition and with the code I did loads but enters an eternal loop. I have already researched and tried everything I saw but I did not get the expected result. If anyone can help me. This code is on the same page that I want to translate.I thank you in advance for the attention.

<script>
    $(window).ready(function(){
        if(pais === 'brazil'){
              document.location = 'minhaurl.com?language=portuguese';                      
        }    
        else{
              document.location = 'minhaurl.com?language=english';            
       }
    }); 
</script>
  • Do you understand why you are entering the loop? What result awaits?

  • If this code is in the index of minhaurl.com, then when loading the page, it runs the script, which arrow Location to minhaurl.com, which runs the script, which arrow Location to minhaurl.com, which runs the script, which arrow Location to minhaurl.com and so on...

  • @Magichat I hope it loads once.

  • @hkotsubo So this I understood but how I could solve?

  • 1

    This is happening because you are using a relative url, put http:// or https:// before the domain. After that you may have more to debug, because it is not clear where the value of pais.

  • "how could I solve?" - Depends, where does the variable come from pais? How/when should I show a particular language? It is the user who chooses, it is some other action of the system that defines it? etc etc etc etc... Without knowing this, any solution will be kick - as the answer below, for example, because it is not possible to know if the situation below applies (if it is to set the language only if it does not exist, or if it can overwrite an existing one, etc.). Could [Dit] the question clarifying these points?

Show 1 more comment

1 answer

1


I believe this will solve. I was just wondering where the variable comes from pais. My solution is not the best possible, but will solve. From what I understand it happens the loop because there is no validation if it has already been set the language for the page and therefore in my solution I search the parameters of the URL and check if it exists

<script>
function getUrlParams(param) {
    var aParams = new Object();
    window.location.search.replace(/[\?&]([^=&]*)(?:=([^&]*))?/gi, function (substr, param, value, offset, str) {
        if (typeof value === 'undefined') {
            value = '';
        }
        aParams[param] = decodeURIComponent(value.replace(/\+/g, ' '));
    });
    if (param) {
        return aParams[param];
    }
    return aParams;
}

$(window).ready(function(){
    //busca os paramêtros da URL. Se não existir o parâmetro ele vai retornar undefined
    var language = getUrlParams('language');
    
    //Verifica se já existe o parâmetro de país. Se já existir não faz sentido fazer o document.location
    if(typeof language === 'undefined'){
        if(pais === 'brazil'){
            document.location = 'minhaurl.com?language=portuguese';
        }
        else{
            document.location = 'minhaurl.com?language=english';
        }
    }        
}); 
  • 1

    Instead of regex, you can use URL.searchParams: https://developer.mozilla.org/en-US/docs/Web/API/URL/searchParams (already compatible with most current browsers)

  • 1

    "If it already exists, it makes no sense to do Document.Location" - what if the page is already in one language and the user wants to switch to another? Of course this was not said in the question, and so I asked for more details (because only with what is there, it is not possible to know if it is or not to be like this)

  • makes sense @hkotsubo .. really lack information to have a good solution

Browser other questions tagged

You are not signed in. Login or sign up in order to post.