M
M
mrbykovoleg2019-04-21 14:06:00
URL Handling
mrbykovoleg, 2019-04-21 14:06:00

How to perform an action if document.referrer is equal to a certain reference?

I'm tracking the link they came from. There are several subdomains, they have different first letters: a.domen.ru, b.domen.ru, c.domen.ru... I want to check from which domain they came in and perform the necessary cycle. In the console it displays the correct link from which you entered. Can't test it and run the loop. The cycle starts constantly last.

<script type="text/javascript">
            let link = document.referrer,
                replace = document.querySelectorAll('.replace'),
                replace_input = document.querySelectorAll('input');
           
            // 100
            
                if (link === 'http://a.domen.ru){ //без http: пробовал
                    for(let i = 0; i < replace.length; i++){
                        //number = номер телефона
                        replace[i].innerHTML = 'number';
                        replace[i].setAttribute('href','tel:number');
                    }
                    
                    for(let i = 0; i < replace_input.length; i++){
                        //устанавливаю value у type="hidden" чтобы получить в письме ссылку с которой пришли
                        if(replace_input[i].getAttribute('type') == 'hidden'){
                            replace_input[i].value = link;
                        }
                    }
                }             

            // 103
            
                if (link === 'http://b.domen.ru'){ //без http: пробовал
                    for(let i = 0; i < replace.length; i++){
                        //number = номер телефона
                        replace[i].innerHTML = 'number';
                        replace[i].setAttribute('href','tel:number');
                    }
                    
                    for(let i = 0; i < replace_input.length; i++){
                        if(replace_input[i].getAttribute('type') == 'hidden'){
                            replace_input[i].value = link;
                        }
                    }
                }  
                
            // console.log(document.referrer.includes(location.host))
        </script>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question