skip to Main Content

I’m sorry if that question is obvious to solve, but I don’t know JavaScript yet but feels like it’s the only way to implement what my clients need for their website.

I want to redirect users with specific browser to the specific simplified page once they visit homepage and keep this redirection work everytime they click on homepage button. Basically what I want is a 301 redirect based on user-agent. I’ve tried many times but all I’ve got was an infinte redirection loop or no result at all.

function get_ya_browser(){
var ua = navigator.userAgent;    
if (ua.search(/YaBrowser/) > 0) document.location.href='redirect-link.html';
return '';
}

How could I change this code to work? Would really appreciate any help.

2

Answers


  1. Try – document.location.replace =’redirect-link.html’;
    instead of document.location.href =’redirect-link.html’;

    .location.replace() simulates a HTTP redirect
    .location.href() simulates a mouseclick

    Login or Signup to reply.
  2. If the code was part of redirect-link.html && ua.search(/YaBrowser/) !== -1, you will get an infinite loop.

    following code solved this problem:

    function get_ya_browser(){
       var ua = navigator.userAgent;    
       if (ua.search(/YaBrowser/) !== -1 && !location.href.includes('elbrus-m-zakaz.ru/home-page-windows-xp/')) {
          document.location.href='//elbrus-m-zakaz.ru/home-page-windows-xp/';
       }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search