简体   繁体   English

每次都重定向到移动网站

[英]Redirect to mobile website every time

I'm using the typical mobile website redirect script that I've seen all over, and it works great. 我使用的是我看到的典型的移动网站重定向脚本,并且效果很好。 I also have a link on my mobile site that lets them view the regular website if desired, and that works as well. 我在移动网站上也有一个链接,可以让他们根据需要查看常规网站,并且效果也不错。

Here's my scenario & problem. 这是我的情况和问题。 They go to my mobile website, then click the link to go to the regular website. 他们转到我的移动网站,然后单击链接转到常规网站。 Then they go visit a totally different site. 然后他们去一个完全不同的站点。 If they type my url again to go to my website, it takes them to the regular website not the mobile site. 如果他们再次输入我的URL进入我的网站,它将带他们到常规网站而不是移动网站。 Each separate time they go to my site I want them to be taken to the mobile. 他们每次单独访问我的网站时,都希望将他们带到手机上。 Can someone who is better at this help me out?? 谁能在这方面做得更好? Thanks! 谢谢!

Here's my script on the regular website: 这是我在常规网站上的脚本:

<script type="text/javascript">
    if (document.location.search.indexOf("skipmobile") >= 0) {
        document.cookie = "skipmobile=1";
    } else if ((document.location.hostname.match(/\.mobi$/) || screen.width < 699) && document.cookie.indexOf("skipmobile") == -1) {
        document.location = "m/";
}

And here's the link from my mobile site: 这是我的移动网站上的链接:

<a href="http://www.mywebsite.com/index.html?skipmobile=1">View Full Website</a>

Let's breakdown your JavaScript code. 让我们分解您的JavaScript代码。

if (document.location.search.indexOf("skipmobile") >= 0) {

If the URL contains the string "skipmobile" then the condition >= 0 is true. 如果URL包含字符串“ skipmobile”,则条件>= 0为true。 Notice that it does not really matter what the value is set to. 请注意,将值设置为什么并不重要。 It is only looking for the existence of the string "skipmobile". 它只是在寻找字符串“ skipmobile”的存在。 When that string is in the URL a cookie is being set document.cookie = "skipmobile=1" . 当该字符串位于URL中时,将设置一个cookie document.cookie = "skipmobile=1" indexOf will return a -1 if the string is not found. 如果找不到字符串, indexOf将返回-1

else if ((document.location.hostname.match(/\.mobi$/) || screen.width < 699) && document.cookie.indexOf("skipmobile") == -1)

Let's break this down into pieces. 让我们将其分解成碎片。 First off, || 首先, || means OR and && means AND. 表示OR, &&表示AND。 So if document.location.hostname.match(/\\.mobi$/) is using a regular expression to search the hostname from the URL for the string ".mobi" at the end. 因此, if document.location.hostname.match(/\\.mobi$/)使用正则表达式从URL的主机名末尾搜索字符串“ .mobi”。 So www.yoursite.mobi would match but www.yoursite.com would not. 因此,www.yoursite.mobi会匹配,但www.yoursite.com不会匹配。 Then we have the OR condition || screen.width < 699 然后我们有OR条件|| screen.width < 699 || screen.width < 699 . || screen.width < 699 So if the hostname ends with ".mobi" OR if the screen dimension's width is less than "699" pixels the first part of this condition will be true. 因此,如果主机名以“ .mobi”结尾或屏幕尺寸的宽度小于“ 699”像素,则此条件的第一部分为true。

The next part of this if statement is document.cookie.indexOf("skipmobile") == -1 . if语句的下一部分是document.cookie.indexOf("skipmobile") == -1 This is checking for the non-existence of a cookie named "skipmobile". 这正在检查是否存在不存在名为“ skipmobile”的cookie。 Notice the -1 . 注意-1 So it is checking that the cookie has NOT been set. 因此,它正在检查未设置cookie。

So put that all together, if the hostname ends in ".mobi" OR the screen width is less than "699" pixels AND the cookie "skipmobile" has not been set then direct the browser to the mobile site, "m/", using this code document.location = "m/" . 所以把所有的一起,如果主机名“的.mobi”结尾屏幕宽度小于‘skipmobile’尚未设置,那么直接在浏览器的移动网站,‘M /’,‘699’的像素饼干,使用此代码document.location = "m/"

It looks like the code is checking for the cookie to determine mobile or full site. 看起来该代码正在检查Cookie,以确定移动网站或完整网站。 Because of this it is "remembering" the setting when they leave and come back to your site. 因此,当他们离开并返回您的站点时,它可以“记住”设置。

When setting your cookie - specifically do not set an expiry date. 设置Cookie时-特别不要设置有效期限。 This is commonly referred to as a "session cookie". 通常将其称为“会话cookie”。 The cookie will automatically expire when the browser is closed. 当浏览器关闭时,cookie将自动过期。

or better yet... 或者更好...

Quite simply, just don't set a cookie at all. 很简单,就是根本不设置cookie。 I don't see anywhere in the code where the actual presence of a cookie does anything... only the absence of the cookie matters. 我没有在代码中的任何地方看到cookie的实际存在执行任何操作……仅cookie的缺失很重要。

furthermore, if you don't set a cookie... there will never be a cookie. 此外,如果您不设置cookie,那么绝对不会有cookie。 Why waste code looking for it? 为什么浪费代码寻找它?

Try this instead... 试试这个...

<script type="text/javascript">
if (document.location.search.indexOf("skipmobile") >= 0)
{
document.location = "m/";
} else if ((document.location.hostname.match(/\.mobi$/) || screen.width < 699))
{
document.location = "m/";
}
</script>

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

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