javascript redirecting mobile devices if referrer is not my domain -


for reasons long explain here, want use javascript redirect visitors index page mobile subdomain if devices using useragent.

the issue have mobile site has "view full site" link sends index page. creates loop condition.

here trying accomplish.


if referring url not own domain and user agent matches, redirect mobile subdomain.


i close missing something.

    if   (document.referrer != "mydomain.com" &&             (navigator.useragent.match(/iphone/i)) ||             (navigator.useragent.match(/ipod/i)) ||             (navigator.useragent.match(/ipad/i)) ||             (navigator.useragent.match(/googlebot-mobile/i))); {         location.replace("https://m.mydomain.com");         } 

your condition bit funny , have semicolon shouldnt.

the referrer condition ignored since or'ed other conditions. put or's in brackets , should work.

if   (document.referrer != "mydomain.com" &&             ((navigator.useragent.match(/iphone/i)) ||             (navigator.useragent.match(/ipod/i)) ||             (navigator.useragent.match(/ipad/i)) ||             (navigator.useragent.match(/googlebot-mobile/i)))) {         location.replace("https://m.mydomain.com");         } 

having link idea since shows youre aware can never second guess user (and device recognition) 100%.

as mplungjan says not robust rely on referrer. recommend store 'session' information registering users intention view on full site. can override switch.

most mobile browsers support sessionstorage use keep track of (eg sessionstorage['site'] = 'full') although i've had issues ios , private mode (doesnt store in private mode).


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -