簡體   English   中英

Android 重定向不起作用

[英]Android redirect does not work

我需要在 javascript 文件中重定向到用戶指定的給定 URI。

所以一個簡單的例子我是如何做到這一點的:

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

這適用於除 Android 設備以外的所有設備。 iOS 和所有現代 Web 瀏覽器都支持 window.location.replace(...),但 Android 設備不支持。

但是,如果我現在嘗試使用此功能重定向,讓我們說“ http://www.google.com ”,Android 設備實際上無法重定向到給定的 url。

現在只是我在這里愚蠢還是有其他問題?

真誠的

ps 重定向函數被調用為來自發送的 XML 請求的回調,但這根本不是問題。

Android 支持沒有href屬性的document.location

嘗試使用:

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

我認為它應該是window.location.href ,而不是document.location.href

我建議:

location.assign('someUrl');

這是一個更好的解決方案,因為它保留了原始文檔的歷史記錄,因此您可以按照此處的說明使用后退按鈕或 history.back() 導航到上一個網頁

您可以在 iOS 中使用 match 和 replace,但顯然不能在 Android 中使用。 根據我的經驗,這適用於 Android 中的重定向:

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

您可以使用: window.location = "http://example.com";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM