0

I checked related posts on this topic and it seems that the solutions are not quite what I am looking for because of the type of device detection code that I am using on my desktop site.

I have a desktop website at mydomain.com and have installed the following script to redirect mobile users to the mobile website at m.mydomain.com function detect() { var uagent = navigator.userAgent.toLowerCase(); var mobile = false; var search_strings = [ "iphone”, "ipod”, "ipad”, "series60”, "symbian”, "android”, "windows ce”, "windows7phone”, "w7p”, "blackberry”, "palm” ]; for (i in search_strings) { if (uagent.search(search_strings[i]) > -1) mobile = true; } return mobile; } if (detect()) window.location = "http://m.mydomain.com/“; The redirect works fine when someone uses http://mydomain.com from a mobile phone and the mobile version is displayed. However now I want to give the option to that mobile phone user to redirect to the full desktop website at domain.com without looping back to the mobile version.

How do I do that without using PHP? Is this possible using Jquery/Javascript? The typical won't work of course.

Thanks in advance..

John G
  • 55
  • 1
  • 7
  • Do you want them to be prompted before redirecting to the mobile site? – reZach Jul 18 '14 at 01:49
  • no, I want to have a link in my mobile site "Switch to Desktop" which when clicked, would take the user on the phone to the desktop version and keep them in that desktop version. there is no problem in bringing up the mobile version on the phone when the desktop url is entered - the above ode takes care of that. – John G Jul 18 '14 at 01:58
  • Do you want the user to stay on the desktop site for the entirety of the visit, have you thought about making a cookie? – reZach Jul 18 '14 at 02:01
  • Well, if the user on the phone chooses to click on that link to go and stay on the desktop version they can. If they want to return back to the mobile version, a simple browser "Back" would be enough. I have not thought about using a cookie but I am open to it if it works of course – John G Jul 18 '14 at 02:04

1 Answers1

0

Make a cookie that keeps track if our user wants a mobile or desktop view. Make the cookie if the user ever wants to view the desktop site. By default, the cookie will persist throughout the entire session.

// If the user want's to view the desktop version
<a href="...url..." onclick="document.cookie = 'desktopviewcookie=true;'">Desktop site</a>

Change your check accordingly (using a nice function found on SO)

function readCookie(name) {
    return (name = new RegExp('(?:^|;\\s*)' + ('' + name).replace(/[-[\]{}   ()*+?.,\\^$|#\s]/g, '\\$&') + '=([^;]*)').exec(document.cookie)) && name[1];
}

// If we have a mobile device and our cookie isn't set we will assume the user wants the mobile page
if (detect() && readCookie("desktopviewcookie") !== "undefined"){
    window.location = "http://m.mydomain.com/“;
}

I didn't test this, but give it a go

Community
  • 1
  • 1
reZach
  • 8,945
  • 12
  • 51
  • 97
  • thank you for the code, but it does not seem to be working. when I click that hyperlink to the desktop URL, it loops me back to the mobile version. – John G Jul 18 '14 at 18:24
  • try null instead of "undefined". Also set a breakpoint and type in "document.cookie" into the console (hit f12) and hit enter to check if the cookie exists. That should help debug this a bit further – reZach Jul 20 '14 at 03:48
  • null is not working either. I am actually trying to come to the desktop version from a smartphone, so there is no F12 key to hit :-) – John G Jul 20 '14 at 19:21
  • You can also emulate a mobile device in chrome, f12, show drawer, emulation and pick your device. Give that a go – reZach Jul 21 '14 at 18:16