【发布时间】: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();
})();
如何使重定向自动工作?
问候
【问题讨论】:
-
在调用
detecmob(DOMContentLoaded,如果我没记错的话)之前,你能不能先等待页面完全加载?我怀疑你的问题出在href元素呈现之前运行的脚本.. -
(function(){}(); 是一个匿名函数,可以用作它的简写,因此页面已完全加载。我已经记录了返回电话号码的 href
-
我认为您不应该强迫用户在访问您的网站时使用移动应用程序。我们在自己的公司犯了这个错误,谷歌通过将我们隐藏在搜索结果中来惩罚我们。然后 ppl 将使用“强制桌面”模式...
标签: javascript redirect mobile