【问题标题】:Redirect from website to mobile phone app从网站重定向到手机应用程序
【发布时间】:2020-05-17 20:43:46
【问题描述】:

有人要求我创建一个脚本,将 移动 用户直接重定向到插入电话号码的电话应用程序。而不是查看网页。

不知何故,我的脚本只适用于点击事件,而不适用于页面加载事件。

我尝试使用 window.open('tel:+612345678') 和 window.location.href = 'tel:+612345678'; 打开手机应用程序但是没有成功我最近的尝试是动态创建一个链接元素,然后调用 window.location.href=document.getElementById('redirectToPhoneElement').href; 但也没有成功。

(function(){
var tel = "tel:+612345678"; // hier telefoonnummer invullen zonder +

function detectmob() {
    if(navigator.userAgent.match(/Android/i)){
        var dRatio = window.devicePixelRatio;
        var width = screen.width * dRatio;
        var height = screen.height * dRatio;
        if(window.innerWidth > window.innerHeight){
         // check landscape max width
            if(height / dRatio <= 480){
                //document.writeln('ls mobile');
                GoToPhone();
            }
        } else {
            if(width / dRatio <= 480){
                //document.writeln('p mobile');
                GoToPhone();
            }
        // check potrait width
        }
    } else if(navigator.userAgent.match(/webOS/i) || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/BlackBerry/i) || navigator.userAgent.match(/Windows Phone/i))
    {
        GoToPhone();
    }
}

function GoToPhone(){
    var telLink = document.createElement("a");
    var telText = document.createTextNode("call");
    telLink.appendChild(telText);
    telLink.href=tel;
    telLink.id="redirectToPhoneElement";
    document.body.append(telLink);

    window.location.href=document.getElementById('redirectToPhoneElement').href;

}
detectmob();
})();

如何使重定向自动工作?

问候

【问题讨论】:

  • 在调用detecmobDOMContentLoaded,如果我没记错的话)之前,你能不能先等待页面完全加载?我怀疑你的问题出在href 元素呈现之前运行的脚本..
  • (function(){}(); 是一个匿名函数,可以用作它的简写,因此页面已完全加载。我已经记录了返回电话号码的 href
  • 我认为您不应该强迫用户在访问您的网站时使用移动应用程序。我们在自己的公司犯了这个错误,谷歌通过将我们隐藏在搜索结果中来惩罚我们。然后 ppl 将使用“强制桌面”模式...

标签: javascript redirect mobile


【解决方案1】:

你可以在加载了href="tel: 3281769362"的窗口调用函数,它也可以在手机上使用

function call() {
  document.getElementById("call").click();
}
window.onload = call;
&lt;a id="call" href="tel:123-456-7890"&gt;Wait&lt;/a&gt;

【讨论】:

  • 它在我的设备上不起作用。在我的笔记本电脑上它工作正常,但在我的安卓设备上却不行
  • function rtCall(){ document.getElementById("redirectToPhoneElement").click(); } window.onload = rtCall;
  • 你为什么发这个?
  • 因为我想也许我打错了。当我将“tel:123-456-7890”更改为“test.html”等其他页面时,重定向工作正常。也许是我的手机或设置?
  • 好的...你不应该强迫人们在页面加载时调用,你应该让他们在自己同意的情况下调用,只是一个友好的建议
猜你喜欢
  • 2014-10-27
  • 2021-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多