使用jquery从Mobile切换到桌面

时间:2014-01-28 14:32:52

标签: jquery html

我正在使用此代码将用户从桌面版本定向到我的移动网站。

if (screen.width < 480) {
    document.location = "Path of Mobile Page"
}

我想让用户可以选择从移动网站切换回桌面版(可能正在使用一个名为“查看桌面版”的主播),但此代码会将其重定向回移动网站。我想知道我是否可以在用户点击锚点后让网址停留在桌面页面上。我对jquery的了解有限,所以任何帮助都会受到高度赞赏。

我想我是新人,所以我无法清楚地表达我的问题,所以我再次解释它。

我有两个单独的页面,index.html和index1.html,用户只知道index.html。在移动设备上查看index.html时,它会重定向到index1.html(因此检测移动设备不是问题)。现在index1.html中有一个链接,它有href =“index.html”但是,当我点击这个链接时,index.html检测到它的移动设备并再次将其重定向回index1.html。这是我想要避免的,一旦用户点击锚点来查看index.html,它就不应该重定向回index1.html。用户第一次输入index.html时,我已经使用上面提到的代码将其重定向到index1.html

由于

3 个答案:

答案 0 :(得分:0)

由于您将有两个不同的路径,只需在“移动”页面上添加一个按钮即可将用户重定向到“桌面”页面。 顺便说一下,您可以检查导航器而不是屏幕宽度来确定要显示的默认页面。

答案 1 :(得分:0)

您可能需要查看有关如何检测移动浏览器的以下主题:

What is the best way to detect a handheld device in jQuery?

如果您不进行整页重定向,则可能需要传递URL参数,如:

document.location = path + "index.html?mode=desktop";

并检查该值,以便检测用户是否“手动选择”移动/桌面站点:

How to Get Url Parameters & Values using jQuery

答案 2 :(得分:0)

  

您只需使用userAgent即可完成此操作。

 function detectmob() { 
 if( navigator.userAgent.match(/Android/i)
 || navigator.userAgent.match(/webOS/i)
 || navigator.userAgent.match(/iPhone/i)
 || navigator.userAgent.match(/iPad/i)
 || navigator.userAgent.match(/iPod/i)
 || navigator.userAgent.match(/BlackBerry/i)
 || navigator.userAgent.match(/Windows Phone/i))   {
    return true;
  }
 else {
    return false;
  }
}
  

如果窗口800x600或更低的分辨率,则它是移动设备。要执行此操作,您可以执行以下操作。

function detectmob() {
   if(window.innerWidth <= 800 && window.innerHeight <= 600) {
 return true;
   } else {
     return false;
   }
}

<强> Refernce: Detect mobile device

谢谢,

相关问题