Android redirect does not work

后端 未结 5 1282
故里飘歌
故里飘歌 2021-01-02 08:03

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(navigat         


        
相关标签:
5条回答
  • 2021-01-02 08:37

    You can use match and replace in iOS but apparently not in Android. In my experience this is what works for redirects 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 讨论(0)
  • 2021-01-02 08:50

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

    0 讨论(0)
  • 2021-01-02 08:51

    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.

    0 讨论(0)
  • 2021-01-02 08:56

    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);
    }
    
    0 讨论(0)
  • 2021-01-02 08:57

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

    0 讨论(0)
提交回复
热议问题