0

I am trying to create a button that sends a text message. I was given code I just don't know what to do with it. Please Help!

iPhone

href = "sms://+15558675309/&body=TextHere"

Android

href = "sms://+15558675309/?body=TextHere"

If you’re wondering, “how can I determine whether they’re using an iPhone or an Android?”

Here ya go:

function getMobileOperatingSystem() {
  var userAgent = navigator.userAgent || navigator.vendor || window.opera;

    if ( userAgent.match( /iPad/i ) || userAgent.match( /iPhone/i ) || userAgent.match( /iPod/i ) ) { return 'iOS'; }

    else if ( userAgent.match( /Android/i ) ) { return 'Android'; }

    else { return 'unknown'; }
}
Then you can wrap the above SMS syntax like this:

var message_text = 'Pre-filled message goes here'

if (device == 'iOS') {
 href = "sms://+15558675309/&body=" + encodeURI(message_text);
} 

if (device == 'Android') {
 href = "sms://+15558675309/?body=" + encodeURI(message_text);
}
  • You want to send an SMS after clicking the button? if so, do you have a panel capable of sending short messages? – Jack Johansson Feb 8 '17 at 2:40
  • this link automatically opens up an iphone's messaging app: sms://+15558675309/&body=TextHere The rest of the code was written to determine which device the website visitor is using. I am trying to make this into a button. – Javier Peña Feb 9 '17 at 2:44
0

To make a clickable button that would open a new windows using your provided link, copy and paste this script to your footer :

<script>
function getMobileOperatingSystem() {
  var userAgent = navigator.userAgent || navigator.vendor || window.opera;

    if ( userAgent.match( /iPad/i ) || userAgent.match( /iPhone/i ) || userAgent.match( /iPod/i ) ) { return 'iOS'; }

    else if ( userAgent.match( /Android/i ) ) { return 'Android'; }

    else { return 'unknown'; }
}

var message_text = 'Pre-filled message goes here'
if (device == 'iOS') {
 href = "sms://+15558675309/&body=" + encodeURI(message_text);
} else {
 href = "sms://+15558675309/?body=" + encodeURI(message_text);}

document.getElementById("myElement").innerHTML = '<button onclick="window.open(' + href + ');">Click me!</button>';
</script>

Then, create a div element and set it's ID to myElement or anything you want, like : <div id="myElement"></div>.

After loading the page, the button will inserted to your div.

  • It needs to be able to recognize the device the visitor is using. That is what the rest of the provided code is for. – Javier Peña Feb 10 '17 at 3:30
  • @JavierPeña You already have a function that does that. Use this button at the end of your code, and it will create a button depending on whether the visitor is using iPhone or Android. – Jack Johansson Feb 10 '17 at 4:10
  • Where do I put the function? – Javier Peña Feb 10 '17 at 19:38
  • I updated the answer for you. – Jack Johansson Feb 11 '17 at 6:57
  • Dude Im so freaking confused.. can we talk on the phone or something? – Javier Peña Feb 14 '17 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.