我設法想出了另一種使用本地存儲的解決方案,對於像我這樣的初學者來說,這非常簡單。這可能是一種業餘的做事方式,但它的確適用於我的目的。
所以更新的代碼對網站的桌面版:
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>
正如我說,也許不是最好的方式,但它肯定有效,對初學者很容易。