【问题标题】:Rails redirect to mobile app using JavascriptRails 使用 Javascript 重定向到移动应用程序
【发布时间】:2020-10-30 07:01:38
【问题描述】:

在我的控制器中,我指定了我希望重定向到的 URL。

def redirect
  @app_url = "myapp://something"
end

在html模板中,我尝试使用JS更改window.location

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>App Redirect</title>
  <script type="text/javascript">
      window.onload = function() {
        const userAgent = window.navigator.userAgent;
        if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i)) {
            window.location = <%= @app_url %>
        }
      }
  </script>
</head>
<body>
    <h1><%= @app_url %></h1>
</body>
</html>

但是,当我转到该页面时,app_url 按预期呈现为&lt;h1&gt;,但没有提示我打开移动应用程序。

但如果我改用window.location = "myapp://something",一切都会按预期进行,因为系统会提示我打开应用程序。

感谢任何帮助。

【问题讨论】:

    标签: javascript html ios ruby-on-rails ruby


    【解决方案1】:

    window.location = "&lt;%= @deep_url %&gt;" 工作

    【讨论】:

      猜你喜欢
      • 2014-04-06
      • 1970-01-01
      • 1970-01-01
      • 2019-01-23
      • 2012-04-20
      • 1970-01-01
      • 2012-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多