怎样在苹果ios系统中,不让alert和confirm弹窗中显示url地址

在做网站开发 或 移动端webapp开发时,我们会经常用到JS的alert和confirm弹窗。在电脑端和android安卓手机上,alert和confirm显示都是正常的,不显示url地址,而唯独在苹果ios系统中会显示url地址。

在苹果ios系统中,如何不让alert和confirm弹窗中显示url地址呢?

我们可以通过JS来重写alert和confirm弹窗的代码来实现。代码如下:

直接将下面代码单独写在js中即可

重写alert方法:

window.alert = function(name){
var iframe = document.createElement("IFRAME");
iframe.style.display="none";
iframe.setAttribute("src", 'data:text/plain,');
document.documentElement.appendChild(iframe);
window.frames[0].window.alert(name);
iframe.parentNode.removeChild(iframe);
};

重写confirm方法:

window.confirm = function (message) {
var iframe = document.createElement("IFRAME");
iframe.style.display = "none";
iframe.setAttribute("src", 'data:text/plain,');
document.documentElement.appendChild(iframe);
var alertFrame = window.frames[0];
var result = alertFrame.window.confirm(message);
iframe.parentNode.removeChild(iframe);
return result;
};

posted @ 2020-01-10 16:16  偶尔z  阅读(937)  评论(0)    收藏  举报