Check URL from document.referrer

0

I have the following situation.

  • When the user accesses the site: www.site.com.br I check if there exists the parameter sc = 2 at the end of the URL, if it exists it can access the site, if it exists it is redirected to www .site.com.br / system / 401

  • On this page www.site.com.br/sistema/401 I saved the page where the user came from:

    var ref = document.referrer

  • On that same page I have a form that the user enters his email to "login". After inserting the email it is redirected to the page it was before through the code below and I enter the sc = 2 parameter at the end of the url:

    window.location.href = ref + (/\?.{1,}=/.test(ref)? '&': '?') + 'sc = 2';

  • But imagine that the user accesses the page directly: www.site.com.br/sistema/401 ie this will be the page that will be saved in the document.referrer there when the person logs in it will be redirected for that same page and the user will be in infinite loop trying to login.

  • How would I check to see if the document.referrer matches a specific page, it would have another URL? EX:

    var ref = document.referrer;
    if (ref == 'https://www.site.com.br/Sistema/401'){ ref = 'https://www.site.com.br'; }
    window.location.href = ref + (/\?.{1,}=/.test(ref) ? '&' : '?') + 'sc=2';
    

    I tried to do the above but did not work where I went wrong? In case when the user logs in:

      

    link

    it should be directed to:

      

    link

    But that's not what happens, it's still being redirected to link

        
    asked by anonymous 30.11.2018 / 17:46

    2 answers

    1

    Just put:

     if (!ref){ ref = 'site.com.br'; }
    

    That worked

        
    07.12.2018 / 17:26
    1

    var x = document.referrer;

    var rfr = document.referrer;
    //mostrando o link em um alerta
    alert(rfr);
    //mostrando o alerta no html de uma div
    $('#ssss').html("O link visitado anteriormente foi : "+ rfr);
    if (rfr=="Site acessado"){
    window.location.href = "Site para carregar";
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><divid="ssss"></div>
        
    30.11.2018 / 18:26