location redirect to "sms:123454" does not work on page load
Asked Answered
G

3

11

Following is the code I have got for location redirect to SMS app when the user is on a mobile browser-

window.onload = function() {
    window.location ="sms:12345?body=" + encodeURIComponent("TEST");
}

This code works perfectly on JS fiddle when running on a mobile JS Fiddle link -https://jsfiddle.net/netstarter/rwqyp2tn/1/

Galvanometer answered 29/5, 2018 at 12:54 Comment(7)
Try document.onloadHomage
If that doesn't help then try with document.onreadyHomage
@TarunLalwani no it does not work with document.onload or document.onreadyGalvanometer
what happens when you manually type the same thing in a navigation bar? does it workHomage
no, it does not work directly from navigation barGalvanometer
Then why do you expect it to work through code?Homage
@TarunLalwani because there might be a way to make it work.it works when used as <a href="SMS:12345?body=dwdwdwdwd">send</a>Galvanometer
A
2

Easiest and appropriate way of doing this would be creating an hidden link and triggering it directly.

window.onload = () => {
  let element = document.getElementById("hiddenAppLink");
  element && element.click();
};
<!DOCTYPE html>
<html>
<body>
<a href='sms:12345?body=${encodeURIComponent("ITR")}' id="hiddenAppLink"></a>
</body>
</html>

You can also trigger it based on a condition by tracking the state if its closed or not in a variable instead bugging the user on every load (You can also use localStorage can't do it in fiddle => security violation). something like this.

window.onload = () => {
  if(window.hideDialouge != true){
      let element = document.getElementById("hiddenAppLink");
      element && element.click();
      //Track if its alredy shown
      window.hideDialouge = true;
  }
};
<html>
    <body>
    <a href='sms:12345?body=${encodeURIComponent("ITR")}' id="hiddenAppLink"></a>
    </body>
    </html>
Arrack answered 5/6, 2018 at 15:46 Comment(1)
might be because of this ... androidpolice.com/2017/11/08/… .... Can't blame google though :).Arrack
O
1

SMS URL seems to be only working when you are about to open new SMS URL. It fails to work when you manually enter the URL or use window.location redirect.

You can use two methods to achieve this:

  1. Open hidden link.

  2. Use window.open (This might ask user to allow popups on your page)

1. Open hidden link.

Create a hidden link and open it.

window.onload = function() {
  let elem = document.getElementById("loadSMS");
  elem && elem.click();
}
<a href="sms:1-111-1111?body=Blah" style="display:none" id="loadSMS">
</a>

2. Use window.open instead of changing window.location

window.onload = function() {
  window.open("sms:1-111-1111?body=Blah");
}
Oeildeboeuf answered 7/6, 2018 at 18:8 Comment(0)
P
0

I had some sms challenges as well. Here was my solution:

let a = document.createElement('a');
a.href = 'sms:?&body=Here is body message';
a.click();

Worked great on all my android and IOS devices!

Philpott answered 28/2, 2024 at 22:10 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.