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/
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>
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:
Open hidden link.
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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With