您当前的位置: 首页 > 
  • 0浏览

    0关注

    1477博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

去除移动端alert/confirm的网址(url)

软件工程小施同学 发布时间:2016-11-17 16:02:22 ,浏览量:0

移动端的alert、confirm都会显示来源的url,影响体验 下面的代码将alert和confirm重写了一遍,可去除url  参考了网上代码,完善了confirm不同状态跳转   示例代码:





文档标题

 


/*remove url of alert/confirm*/  
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);  
    }  
} 

var r=confirm("Press a button");
if (r==true)
{
	document.write("You pressed OK!");
}
else
{
	document.write("You pressed Cancel!");
}



 

关注
打赏
1665320866
查看更多评论
立即登录/注册

微信扫码登录

0.0402s