Search code examples
javascriptipadmobiledetection

how to redirect mobile website to normal version


i actually made a website with a mobile version (in an other folder with a second index.html)

i've used this script to redirect mobile from desktop version to the mobile version

if ( navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/iPad/i) ){
    window.location.href = "http://m.website.net" + document.location.hash;
}    

that work well but the problem is that people who share the mobile version send people to mobile, due to mobile url. So i try to redirect them automatically to the desktop version like this

if (! navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/iPad/i) ){
    window.location.href = "http://www.website.net" + document.location.hash;
}

that worked on iphone but create an infinite loop with ipad, how can i do please ? i can only use javascript to do this

i can't use script like if (screen.width <= 960) {document.location = mobile.html";} because of ipad retina and other resolutions


Solution

  • That will also cause infinite loop in iPods. You are missing !'s in your second script it should be.

    if (!navigator.userAgent.match(/iPhone/i) && !navigator.userAgent.match(/iPod/i) && !navigator.userAgent.match(/iPad/i) ){
        window.location.href = "http://www.website.net" + document.location.hash;
    }
    

    you also need to change ||'s to &&'s since it should redirect if none of them(iPod,iPad,iPhone) matches.