Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android redirect does not work

I need to redirect in a javascript file to a given URI specified by the user.

So a quick example how I do this:

function redirect(uri) {
  if(navigator.userAgent.match(/Android/i)) {
    document.location.href=uri;      
  }
  window.location.replace(uri);
}

This works fine for everything except Android devices. iOS and all modern Webbrowsers support window.location.replace(...), however Android devices don't do that.

But if I now try to redirect using this function, lets say to "http://www.google.com" the android devices fail to actually redirect to the given url.

Now is it just me being stupid here right now or is there another problem?

Sincerly

p.s. the redirect function is called as an callback from an XML request sent, but that should not be an issue at all.

like image 970
cschaeffler Avatar asked Dec 31 '13 13:12

cschaeffler


3 Answers

Android supports document.location without the href property

Try to use:

function redirect(uri) {
  if(navigator.userAgent.match(/Android/i)) 
    document.location=uri;      
  else
    window.location.replace(uri);
}
like image 195
StarsSky Avatar answered Oct 07 '22 19:10

StarsSky


I think it should be window.location.href, not document.location.href.

like image 29
Ridcully Avatar answered Oct 07 '22 20:10

Ridcully


I would suggest :

location.assign('someUrl');

It's a better solution as it keeps history of the original document, so you can navigate to the previous webpage using back-button or history.back() as explained here.

like image 41
AlvynFash Avatar answered Oct 07 '22 20:10

AlvynFash