[英]How can I create a “view to desktop version” link on mobile site without it looping back to mobile version when it resolves?
I've recently created a separate mobile skin for a website. 我最近为网站创建了单独的移动设备外观。 The site serves the mobile version of a page based on the screen size of the device it is being viewed on using the following code.
该站点使用以下代码根据正在查看的设备的屏幕尺寸来提供页面的移动版本。
<script type="text/javascript">
if (screen.width <= 600) {
window.location = "mobile/";
}
</script>
I'd now like to add a "view desktop version" link at the bottom of the page. 我现在想在页面底部添加一个“查看桌面版本”链接。 Naturally, with the above code in the header of each page, it just detects the screen size again and loops back.
当然,在每个页面的标题中都包含上面的代码,它只是再次检测屏幕大小并循环返回。
Could someone please suggest how I could get around this. 有人可以建议我如何解决这个问题。 I suspect this will be a session or a cookie but I'm very new to java and don't know how to set these up.
我怀疑这将是一个会话或一个cookie,但是我对java还是很陌生,不知道如何设置它们。
thanks in advance for any advice. 在此先感谢您的任何建议。
This should be handled by the viewport in the metatag of your website. 这应该由网站的metatag中的视口处理。 The use of jquery can allow users to opt out of responsive design:
使用jquery可以使用户选择退出响应式设计:
var targetWidth = 980;
$('#view-full').bind('click', function(){
$('meta[name="viewport"]').attr('content', 'width=' + targetWidth);
});
To detect if link was clicked you can: 要检测是否单击了链接,您可以:
I've managed to come up with another solution using local storage which is really simple for a beginner like me. 我设法提出了另一个使用本地存储的解决方案,对于像我这样的初学者来说,这真的很简单。 It's probably an amateurish way of doing things but it certainly works for my purposes.
这可能是一种业余的做事方式,但对于我的目的当然是有效的。
So updated the code on the desktop version of the site to: 因此,将网站的桌面版本上的代码更新为:
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";
}
}
then added code to the mobile version of the site to check if the user has previously requested the desktop version: 然后将代码添加到网站的移动版本中,以检查用户以前是否请求过桌面版本:
var GetDesk = localStorage.getItem('GetDesk');
if (GetDesk == 1 ) {
window.location = "/desktop.html";
}
Then at the bottom of the mobile version added the button: 然后在移动版本的底部添加了按钮:
<!-- 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>
As I say, perhaps not the best way but it certainly works and is easy for beginners. 就像我说的那样,也许不是最好的方法,但是它确实有效并且对初学者来说很容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.