1

I'm making a mobile page where a user who needs a quick way to copy an address so they can open their preferred maps app.

Other solutions like selecting the whole address to copy manually or using "if apple/android user click this" are all I can think of using, but I want to avoid. I need an all-device, compatible solution and JS is a friend to all.

Here is the best method that i adapted from this answer: https://stackoverflow.com/a/18899718/2887660

function openAddress(){
// If it's an iPhone..
if( (navigator.platform.indexOf("iPhone") != -1) 
    || (navigator.platform.indexOf("iPod") != -1))
     window.open("maps://maps.google.com/maps?daddr=lat,long&ll=");
else
     window.open("http://maps.google.com/maps?daddr=lat,long&ll=");
}

html

<p>123 Example Road</p>
<a onclick="openAddress()">Touch to Copy Address</a>
Community
  • 1
  • 1
BVSK
  • 133
  • 2
  • 12

1 Answers1

0

Not sure if this will work on mobile, but this works in Chrome Desktop: document.execCommand("copy", false, null);. Remember that you have to select the text first before you can perform a copy (obj.select()).

bvx89
  • 868
  • 5
  • 12
  • what if i know the text that I want to use? – BVSK Dec 03 '13 at 20:11
  • Due to browser security you still have to select the text first before you can make a copy. You can deselect it after it has been copied. – bvx89 Dec 03 '13 at 20:16