简体   繁体   English

如何在移动网站上创建“查看桌面版本”链接,而在解析后又不会循环回到移动版本?

[英]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);
});

See this link for more clarification. 有关更多说明,请参见此链接。

To detect if link was clicked you can: 要检测是否单击了链接,您可以:

  • Add a specific query parameter (like ?forceDesktop=true) which should be removed if returned to mobile 添加特定的查询参数(如?forceDesktop = true),如果返回到移动设备,则应删除该参数
  • Use media queries and single skin (see bootstrap) 使用媒体查询和单一外观(请参阅引导程序)
  • Maybe look for more elaborate version to detect mobile ( link ) 也许寻找更精致的版本来检测手机( 链接
  • Chrome for Android has option to request desktop site ( How to request desktop Chrome for Android可以选择请求桌面网站( 如何请求桌面

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.

相关问题 如何在移动网站上创建“桌面版本”链接而不将其重定向到移动网站 - How to create a “Desktop Version” link on mobile website without it redirecting to the mobile site 如何通过移动版本向网站的桌面版本发出 AJAX 请求 - How to make a AJAX request to a Desktop version of a site via the Mobile version 如何在中预览移动网站 <iframe> 在引导模式下站点的桌面版本中 - How to preview mobile site in <iframe> in desktop version of site in bootstrap modal 通过Mobile访问时加载Mobile Version,否则加载Desktop Version - Load Mobile Version when visited with Mobile otherwise load Desktop Version 如何仅在移动设备上显示桌面版本 - Blogger - How to only show desktop version on mobile - Blogger 何时需要无缝启动网站的移动版本? - Seamless start of mobile version of site when needed? 如何在本地(通过扩展程序)请求网站的移动版本? - How can I request mobile version of a site locally (from my extension)? 将移动用户重定向到桌面版本 - Redirecting a mobile user to a desktop version 如何将电话号码显示为移动设备而不是桌面设备上的链接? - How can I display a phone number as a link on mobile but not desktop? 表单向自身提交:如何创建可在移动设备上使用的反向链接? - Form submits to itself: how to I create a back link that works on mobile?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM