如何更新 href 并打开 ios 应用程序?

how do I update href and open an ios app?

我想使用 javascript 更改 href,然后打开 ios 应用程序。随附代码中标记为 "third" 的项目会按预期打开应用程序。标记为 "first" 或 "second" 的代码将打开应用程序,但不会传达所需的坐标。我不明白为什么它们不能正常工作,因为 href 文本在每种情况下的格式似乎都相同。

<!DOCTYPE HTML>
<html>
<body>
<p><a id="myAnchor" href="">first</a></p>

<button onclick="secondFunction()">second</button>

<p><a href="gaiagps://map?lat=38.721&amp;lng=-122.819&amp;z=14">third</a></p>

<script>
    var text = 'gaiagps://map?lat=31.933&amp;lng=-95.236&amp;z=14';
    firstFunction();

    function firstFunction() {      
        document.getElementById("myAnchor").href = text;
    }

    function secondFunction() {
      location.replace(text)
    }

</script>
</body>
</html>

您只需要在输入原始 HTML 时使用 &amp;;浏览器将为您将 &amp; 转换为 &

改变

var text = 'gaiagps://map?lat=31.933&amp;lng=-95.236&amp;z=14';
var text = 'gaiagps://map?lat=31.933&lng=-95.236&z=14';

并且您的两个函数应该按照您的预期进行。

另外,您可以在浏览器控制台中输入

document.getElementsByTagName('p')[1].children[0].href
查看浏览器将您的“&”转换为“&”符号。