本地存储被广泛支持,所以让我们使用它。无需打扰饼干。
如果我们做这样的事情上单击在移动网站上显示的“桌面版”链接:
localStorage.setItem("forceToDesktop", "true")
// Followed by redirect to desktop with JS
我们修改我们的屏幕宽度检查,包括上述值的检查:
if (localStorage.forceToDesktop !== "true" && screen.width <= 800) {
// Do redirect stuff
}
这将显示在移动站点如果forceToDesktop值具有不被设置,并且如果屏幕宽度小于或等于800。
但是,仍然有一部分难题缺失。在选择仅查看桌面站点后,移动用户如何回到移动站点?
我们需要以某种方式删除forceToDesktop值。我会做这样的事情。
if (localStorage.forceToDesktop === "true" && screen.width <= 800) {
// Add a link to the page called something like "view mobile site",
// and have it run the below javascript function on click
var backToMobile = function() {
localStorage.removeItem("forceToDesktop");
// Redirect back to the mobile version of the page,
// or just redirect back to this page, and let the normal
// mobile redirect do its thing.
}
}
设置一个cookie,将其标记为手机或桌面? – TankorSmash