【问题标题】:Javascript stop a redirectJavascript 停止重定向
【发布时间】:2012-02-26 13:09:27
【问题描述】:

我有一个脚本,它检测到一个 android 设备然后重定向到“android.html”。在“android.html”页面上,您可以下载应用程序或单击“CONTINUE”继续“index.html”。问题是当您单击“CONTINUE”时,您会陷入无限重定向循环回到“android.html”。如何使单击“CONTINUE”忽略浏览器检测重定向脚本?

index.html javascript android 检测重定向

var isAndroid = ua.indexOf("android") > -1;
if(isAndroid) {
   window.location = 'android.html';
}

android.html

<html>
<head>
</head>
<body>
If you are not using Dolphin Browser HD.</br>
<a href="dbs/js/DolphinbrowserV7.3.1beta.apk">CLICK HERE TO INSTALL</a></br></br>
<a href="index.html">CONTINUE</a>
</body>
</html>

【问题讨论】:

  • 所以在索引页面上你重定向到 android.html 然后继续按钮链接回索引?
  • 是的,我希望他们被重定向,然后在他们看到重定向页面后,他们可以继续返回 index.html
  • @blainer,看看我的回答应该可以解决你的问题,在查询字符串中保留一些值并检查相同的值,这样你就可以知道你来自那个页面

标签: javascript jquery android cookies redirect


【解决方案1】:

你可以使用 javascript 的 localStorage。

index.html

var isAndroid = ua.indexOf("android") > -1;
if(isAndroid) {
 localStorage.getItem('shouldContinue') ?/*nothing*/:window.location = 'android.html';
}

android.html

<a href="index.html" onclick="localStorage.setItem('shouldContinue',1);">CONTINUE</a>

【讨论】:

  • localStorage.getItem('shouldContinue') ? continue; 不能工作有几个原因(首先,你没有处于循环中,所以continue 没有任何意义并且会中断。其次,你没有 else 用于三元条件并将中断)。 if(isAndroid &amp;&amp; !localStorage.getItem('shouldContinue')) 可能是什么意思..
  • @rgthree - 感谢您指出我在使用 continue 而不是循环中的错误 :) 编辑后的版本反映了更改。
  • @TraviJ 实际上,这也会中断,因为您的三元条件中没有 true 表达式...不知道为什么您不想放置 @987654326 @签入if(),就像我上面提到的那样,但如果你真的想在那条线上完成所有事情,你可以这样做:localStorage.getItem('shouldContinue') || (window.location = 'android.html');
【解决方案2】:

一个参数可能会起作用,但现在您需要更改所有指向您的index.html 页面的链接以传递它...恶心。你真正想要做的是设置一个你可以在两边都检查的cookie。 (我假设这是您期待的响应,因为您已使用 cookies 标记您的问题)。

既然你已经标记了你的问题jquery,我会推荐https://github.com/carhartl/jquery-cookie(虽然我从未使用过)并且:

android.html

If you are not using Dolphin Browser HD.</br>
<a href="dbs/js/DolphinbrowserV7.3.1beta.apk">CLICK HERE TO INSTALL</a></br></br>
<a href="index.html" onclick="$.cookie('forceAndroid', true);">CONTINUE</a>

index.html

var isAndroid = ua.indexOf("android") > -1;
if(isAndroid && !$.cookie('forceAndroid')){
   window.location = 'android.html';
}

我的 JS 不使用 jQuery,但这里有一个关于库的好文档(我假设 jQuery 没有内置 cookie 支持?):http://www.electrictoolbox.com/jquery-cookies/

【讨论】:

    【解决方案3】:

    我不知道你是否可以,但一种解决方案是在查询字符串中使用a参数,例如:

    var isAndroid = ua.indexOf("android") > -1;
    var isContinue = window.location.href.split(/ParameterName=/)[1];
    if(isAndroid && notContinue) {
       window.location = 'android.html';
    }
    

    并且,在继续按钮中:

    <a href="index.html?ParameterName=redirect">CONTINUE</a>
    

    请注意,我获取字符串值的方式无论如何都不能防错,你可以看看this answer

    【讨论】:

      【解决方案4】:
      var isAndroid = ua.indexOf("android") > -1;
      var fromAndroidPage = (location.search.indexOf("fromAndroidPage=yes") != -1);
      
      if(fromAndroidPage && isAndroid) {
         window.location = 'android.html';
      }
      

      将您的页面更改为

      <a href="index.html?fromAndroidPage=yes">CONTINUE</a>
      

      【讨论】:

        【解决方案5】:

        在 url 中加入一些查询字符串

        <html>
        <head>
        </head>
        <body>
        If you are not using Dolphin Browser HD.</br>
        <a href="dbs/js/DolphinbrowserV7.3.1beta.apk">CLICK HERE TO INSTALL</a></br></br>
        <a href="index.html?test=a">CONTINUE</a>
        </body>
        </html>
        

        并在您的 javascript 中编写逻辑

        var url=location.href;
        var isValue=url.indexOf('?test=a') // -1 says its not there.
        
        var isAndroid = ua.indexOf("android") > -1;
        if(isAndroid && isValue==-1) {
           window.location = 'android.html';
        }
        

        【讨论】:

          猜你喜欢
          • 2013-06-09
          • 2012-09-19
          • 1970-01-01
          • 2014-03-31
          • 1970-01-01
          • 2015-12-28
          • 2017-04-17
          • 2012-06-04
          • 2014-04-21
          相关资源
          最近更新 更多