Search code examples
ioshref

how do I update href and open an ios app?


I want to change an href using javascript and then open an ios app. The item labeled "third" in the enclosed code opens the app as anticipated. The code labeled "first" or "second" will open the app, but do not communicate the desired coordinates. I don't understand why they do not function correctly since the href text seems identically formatted in each case.

<!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>

Solution

  • You only need to use &amp; when you are typing raw HTML; the browser will convert &amp; to & for you.

    Change

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

    and your two functions should do what you intended.

    As an added aside, in the browser console you could type

    document.getElementsByTagName('p')[1].children[0].href
    
    to see that the browser converted your `&`'s to ampersands.