使用JavaScript触发电话呼叫

12
我想仅使用Javascript来触发tel:12345 HREF,而不是使用锚点。我只想获取输入字段的值,并使用JS提示用户拨打输入的号码。我的触发器将是位于该字段旁边的按钮。
有什么建议吗?

你尝试过什么吗? - Harsha Venkataramu
Tadaaaaa!https://developer.mozilla.org/zh-CN/docs/Web/API/Window - elclanrs
6
有些流氓将在那个输入框中输入911。 - Dan Ross
或者更糟糕的是,一些僵尸网络将代表其所有被控制用户对其进行攻击。 - Dan Ross
3个回答

23

使用:

window.open('tel:12345');

19
那个最佳的解决方案是什么,还是window.location.href='tel:12345';,哪一个更好? - adrianTNT
10
我喜欢你的解决方案,因为它不会打开新窗口或标签页,而是直接在当前窗口中拨打电话。 - chrislebaron

1
<button onclick="myFunction()">
Call
</button>

function myFunction(){
var a = document.getElementById('input').value;
window.location.href = 'tel:' + a;
}
You can also view the live version on my website:
https://www.theharnishes.com/phone.html

Here we make a button and assign a function to it that bassically select the input, get the value, and the window.location.href will have the value of the input and it'll sent the url to call any number. 

1
请不要仅仅发布代码作为答案,还要提供代码的解释以及它如何解决问题。带有解释的答案通常更有帮助和更高质量,并且更有可能吸引赞同。 - Hasip Timurtas

-1

JS调用非常简单。 使用我们的:onclick函数,Location和tel 我们可以在任何标签中实现, 窗口。位置('tel:number')

点击我拨打199

1
请在您的回答中提供更多细节。目前的写法让人难以理解您的解决方案。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接