Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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/

like image 842
NetStarter Avatar asked May 29 '18 12:05

NetStarter


2 Answers

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>
like image 187
karthik Avatar answered Sep 17 '22 14:09

karthik


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");
}
like image 40
Navjot Ahuja Avatar answered Sep 18 '22 14:09

Navjot Ahuja