移动网站到桌面网站没有循环回Javascript / Jquery

时间:2014-07-18 01:45:16

标签: jquery mobile

我检查过这个主题的相关帖子,似乎解决方案并不是我想要的,因为我在桌面网站上使用的设备检测代码类型。

我在mydomain.com上有一个桌面网站,并安装了以下脚本,将移动用户重定向到移动网站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/“; 当有人使用手机中的http://mydomain.com并显示移动版时,重定向工作正常。但是现在我想让该手机用户选择重定向到domain.com上的完整桌面网站,而不会回到移动版本。

如何在不使用PHP的情况下执行此操作?这可能使用Jquery / Javascript吗?典型的当然不会起作用。

提前致谢..

1 个答案:

答案 0 :(得分:0)

如果我们的用户想要移动或桌面视图,请创建一个跟踪的Cookie。如果用户想要查看桌面站点,请创建cookie。默认情况下,cookie将在整个会话期间保持不变。

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

相应地更改支票(使用一个很好的功能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/“;
}

我没有测试过这个,但是试一试