我设法想出了另一种使用本地存储的解决方案,对于像我这样的初学者来说,这非常简单。这可能是一种业余的做事方式,但它的确适用于我的目的。
所以更新的代码对网站的桌面版:
var GetDesk = 0;
var GetDesk = localStorage.getItem('GetDesk');
//check screen size is less than 600
if (screen.width <= 600) {
//check if there's anything in local storage showing the users requested the desktop
if (GetDesk == 0) {
window.location = "/mobile.html";
}
}
然后添加代码到网站的移动版本,查看用户是否先前请求桌面版:
var GetDesk = localStorage.getItem('GetDesk');
if (GetDesk == 1) {
window.location = "/desktop.html";
}
在的移动版本底部
然后添加的按钮:
<!-- onclick set the value of GetDesk to 1 and redirect user to desktop site -->
<button onclick="localStorage.setItem('GetDesk','1'); window.location.href = '/desktop.html';">View desktop</button>
正如我说,也许不是最好的方式,但它肯定有效,对初学者很容易。