简体   繁体   English

移动到普通网站重定向

[英]Mobile to Normal website Redirect

I have a CV/Resume site and when a user visits my website and the screen resolution is lower than 960 I redirect him to my mobile site through this JS 我有一个简历/简历网站,当用户访问我的网站并且屏幕分辨率低于960时,我通过这个JS将他重定向到我的移动网站

    <script type="text/javascript">if (screen.width <= 960) {document.location = mobile.html";}</script>

My question is if the user wants to visit the normal site and hit a link the javascript will run again and it will redirect the user back to the mobile site. 我的问题是,如果用户想访问普通网站并点击链接,javascript将再次运行,它会将用户重定向回移动网站。 One method to solve this problem it could be to duplicate the normal site lets say index.html and index1.html one with the above code and one with no JS but i think this solution is so noobish. 解决这个问题的一种方法可能是复制普通网站让我们说index.html和index1.html一个上面的代码和一个没有JS,但我认为这个解决方案是如此的愚蠢。 Any way to solve this problem? 有什么方法可以解决这个问题? Before you answer please note that i have no access to the .htaccess file. 在您回答之前请注意我无法访问.htaccess文件。

My problem is when a user visits my site with scree width lower than 960 go to mobile site but if he wants to see the full site press a link and get redirected to the full site no matter the screen width. 我的问题是,当用户访问我的网站时,网页宽度低于960,请访问移动网站,但如果他想查看完整网站,请按链接并重定向到完整网站,无论屏幕宽度如何。

Here's a small code snippet. 这是一个小代码片段。 When the user changes from mobile to normal, run the following code: 当用户从移动设备更改为正常时,请运行以下代码:

var expires = new Date();
expires.setHours(expires.getHours + 24); 

document.cookie = (document.cookie ? document.cookie + ' ;' : '')
     + ' disableMobile=1; expires=' + expires.toGMTString();

and then change your redirect condition to 然后将重定向条件更改为

   if (screen.width <= 960 && document.cookie.indexOf('disableMobile') < 0) [...]

Two ways come to mind: 想到两种方式:

  1. Use a cookie, so that the user's preference to stay on the full site will be remembered. 使用cookie,以便记住用户留在完整网站上的偏好。 Create/set the cookie just before redirecting. 在重定向之前创建/设置cookie。
  2. Use a query parameter, <a href="index.html?fullsite=true">Full site</a> (or similar), which you set only in the link from mobile.html back to the normal site. 使用<a href="index.html?fullsite=true">Full site</a> (或类似)的查询参数,您只能在从mobile.html链接到正常网站的链接中设置该mobile.html

Then add a condition to your existing screen width test so that it only runs if the cookie/query parameter is not set. 然后在现有的屏幕宽度测试中添加一个条件,以便仅在未设置cookie / query参数时才运行它。

(You may also like to add an extra link on your main index page to let the user choose the mobile version. Obviously if you go the cookie route this should clear the cookie.) (您可能还想在主索引页面上添加一个额外的链接,让用户选择移动版本。显然,如果你去cookie路径,这应该清除cookie。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM