Android redirect not working

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

So a quick example of how I do this:

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

This is great for everyone except Android devices. iOS and all modern Webbrowsers support window.location.replace (...), however Android devices do not.

But if I try to redirect this function now, say, " http://www.google.com ", Android devices cannot actually be redirected to this url.

Now am I just stupid here or is there another problem?

Sincerly

ps the redirection function is called as a callback to the sent XML request, but this should not be a problem at all.

+6
source share
5 answers

Android supports document.location without href property

Try using:

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

For more information, click here

+18
source

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

+1
source

You can use match and replace in iOS, but apparently not in Android. In my experience, this is what works for redirection in Android:

 <script type="text/javascript"> // <![CDATA[ if ( (navigator.userAgent.indexOf('Android') != -1) ) { document.location = "http://www.your URL/your HTML file.html"; } // ]]> </script> 
0
source

You can simply use: window.location = "http://example.com";

0
source

I would suggest:

 location.assign('someUrl'); 

This is the best solution because it stores the history of the original document, so you can go to the previous web page using the back button or history.back (), as described here.

0
source

All Articles