0

I have a script that detects an android device then redirects to "android.html". On the "android.html" page you can either download an app or click "CONTINUE" to continue to "index.html". The problem is when you click "CONTINUE", you get stuck in an infinite redirect loop back to the "android.html. How to i make clicking "CONTINUE" ignore the browser detection redirect script?

index.html javascript android detection redirect

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>
Blainer
  • 2,552
  • 10
  • 32
  • 39
  • So on the index page you are redirecting to android.html then the continue button links back to index? – Jack Feb 03 '12 at 23:14
  • yes, I want them to be redirected then after they see the redirect page they can continue back to the index.html – Blainer Feb 03 '12 at 23:15
  • @blainer , see my answer that should fix your problem , keep some value in the querystring and check for the same , that way you can know you are coming from that page – kobe Feb 03 '12 at 23:22

5 Answers5

0

Put some querystring in the 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>

and in your javascript write the logic

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';
}
kobe
  • 15,671
  • 15
  • 64
  • 91
0
var isAndroid = ua.indexOf("android") > -1;
var fromAndroidPage = (location.search.indexOf("fromAndroidPage=yes") != -1);

if(fromAndroidPage && isAndroid) {
   window.location = 'android.html';
}

Change your page to

<a href="index.html?fromAndroidPage=yes">CONTINUE</a>
amit_g
  • 30,880
  • 8
  • 61
  • 118
0

I don't know if you can, but one solution is using the a parameter in the querystring, for example:

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

and, in the continue button:

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

Please note that the way I get the string value is not error proof by any means, you could take a look at this answer

Community
  • 1
  • 1
nicosantangelo
  • 13,216
  • 3
  • 33
  • 47
0

You could use javascript's 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>
Travis J
  • 81,153
  • 41
  • 202
  • 273
  • `localStorage.getItem('shouldContinue') ? continue;` won't work for a few reasons (First, you're not in a loop, so `continue` means nothing and will break. Secondly, you have no *else* for the ternary condition and will break). `if(isAndroid && !localStorage.getItem('shouldContinue'))` is probably what meant.. – rgthree Feb 03 '12 at 23:43
  • @rgthree - Thank you for pointing out my mistake with using continue not in a loop :) The edited version reflects the changes. – Travis J Feb 03 '12 at 23:55
  • @TraviJ Actually, that's going to break also because you have no *true* expression in your ternary conditional... Not sure why you don't want to put the `shouldContinue` check in the `if()` like I mentioned above, but if you *really* want to do it all on that line you could do: `localStorage.getItem('shouldContinue') || (window.location = 'android.html');` – rgthree Feb 05 '12 at 16:55
0

A paramater might work, but now you'll want to change ALL links to your index.html page to pass that around... gross. What you actually want to do is set a cookie that you can check on both sides. (I'm assuming that's the response you're anticipating since you've tagged your question with cookies).

Since you've tagged your question jquery, I'll recommend https://github.com/carhartl/jquery-cookie (though I've never used it) and:

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';
}

I don't use jQuery for my JS, but here's a good doc on the library (I'm assuming that jQuery doesn't have built-in cookie support?): http://www.electrictoolbox.com/jquery-cookies/

rgthree
  • 7,217
  • 17
  • 21