codetc - 网站开发技术 首页 前端 查看内容

去除移动端alert或confirm的url地址

2018-7-6 16:54| 发布者: CODETC| 查看: 3166| 评论: 0

移动端的使用alert、confirm都会显示来源的url,既不美观又影响体验,解决办法是重写alert和confirm。

<script type="text/javascript">
var wAlert = window.alert;  
window.alert = function (message) {  
    try {  
        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 iwindow = alertFrame.window;  
        if (iwindow == undefined) {  
            iwindow = alertFrame.contentWindow;  
        }  
        iwindow.alert(message);  
        iframe.parentNode.removeChild(iframe);  
    }  
    catch (exc) {  
        return wAlert(message);  
    }  
} 

var wConfirm = window.confirm;  
window.confirm = function (message) {  
    try {  
        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 iwindow = alertFrame.window;  
        if (iwindow == undefined) {  
            iwindow = alertFrame.contentWindow;  
        }  
        var result=iwindow.confirm(message);  
        iframe.parentNode.removeChild(iframe);  
        return result;
    }  
    catch (exc) {  
        return wConfirm(message);  
    }  
} 
</script>
文章来源 CODETC,欢迎分享,转载请注明地址: http://www.codetc.com/article-345-1.html

最新评论

 作为游客发表评论,请输入您的昵称

返回顶部