检测浏览器是否自动屏蔽了弹出窗口

来源:互联网 发布:阿里云ecs 支持.net 编辑:程序博客网 时间:2024/04/30 01:06

现在类似Google Toolbar的工具条几乎都提供了弹出窗口阻挡的功能,但是在实际WEB开发工作中许多情况下还是需要自动谈出窗口,因此需要检测并提醒用户禁用该功能。

在使用MSN Webmessenger时,发现它可以告诉我使用了弹出窗口阻挡功能,在分析它的代码后,提取出下面的程序,可以直接在自己的项目中使用,经过检验,确实可以检测到IE中google等工具条的阻挡情况,但是GoSurf等多线程浏览器的阻挡弹出页面阻挡却没有检测出来,我将使用多种浏览器做一个测试,将结果加上来。

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD><TITLE>弹出窗口检查</TITLE><META http-equiv=Content-Type content="text/html; charset=utf-8">
<SCRIPT language=javascript>function openwindow(u,n,w,h){    var optionstring = "height=" + h + ",width=" + w + ",menubar=no,titlebar=yes,resizable=yes,scrollbars=no,status=no,toolbar=no";                            window.open(u,n,optionstring);}var    spbw = false;function ShowBlockerWarning(){    return spbw;}function Init(){    if (true == Detect())    {        spbw = true;    }}function Detect(){    var e = false;    var pw1 = null;    var pw2 = null;    try    {        do        {            var d = new Date();            var wName = "ptest_" + d.getTime();            var testUrl = IsYBI() ? "popupTest.aspx" : "";            pw1 = window.open(testUrl,wName,"width=0,height=0,left=5000,top=5000",true);            if (null == pw1 || true == pw1.closed)            {                e = true;                break;            }            pw2 = window.open(testUrl,wName,"width=0,height=0");            if (null == pw2 || true == pw2.closed)            {                e = true;                break;            }            if (IsYBI())            {                if (pw1 != pw2)                {                    e = true;                    break;                }            }            pw1.close();            pw2.close();            pw1 = pw2 = null;        }        while(false);    }    catch(ex)    {        e = true;    }    if (null != pw1)    {        try { if (!pw1.closed) pw1.close(); } catch(ex){}    }    if (null != pw2)    {        try { if (!pw2.closed) pw2.close(); } catch(ex){}    }    return e;}function IsYBI(){    var i = false;    try    {        var xObj = new ActiveXObject("YBIOCtrl.CompanionBHO");        if (xObj==null) i = false;        else i = true;    }    catch(ex) { i = false; }    return i;}Init();</SCRIPT><SCRIPT language=javascript>var warningMessage = "检查到使用了弹出窗口阻挡器!Pop-up blocker detected.";var warningInformation = "请确保浏览器允许本网站打开弹出窗口。";var g_blockerHtml;g_blockerHtml  = "<div><table border=1 cellspacing=0 cellpadding=0>";g_blockerHtml += "<tr><td>" + warningMessage + "</td></tr>";g_blockerHtml += "<tr><td>" + warningInformation + "</td></tr>";g_blockerHtml += "</table></div>";</SCRIPT><SCRIPT language=javascript>var messengerWindow = null;function OnLoad_WelcomePage(){        if ( ShowBlockerWarning() )        {            var messageObject = document.getElementById("noPopHtml");            if (null != messageObject)            {                messageObject.innerHTML = g_blockerHtml;            }        }}</SCRIPT>
</HEAD><BODY onload=OnLoad_WelcomePage();><SPAN id=noPopHtml></BODY></HTML>

上面的没有测试过

下面自己写的:

<script language="javascript">        //    检测        var openWin = window.open("Login.aspx","信息系统","location=no,resizable=yes,status=yes,menubar=no,center=yes,scrollbars=no,left=0,top=0");        if ( openWin != null )        {            openWin .resizeTo(screen.availWidth,screen.availHeight);            window.opener = 'X';            window.open('','_parent','');            window.close();        }        else        {            alert('对不起,请关闭阻止弹出窗口程序,或者将本网站加入到受信任站点!');//            var host = top.location.host;//            var path = top.location.pathname;//            path = path.substr(1,path.length);//            path = path.substr(0,path.indexOf('/'));//            var url = 'http://' + host + '/' + path + '/Default/LogOut.aspx';//            top.location.href=url;        }//        if ( top.window.name != "信息系统" )//        {//            window.opener = 'X';//            window.open('','_parent','');//            window.close();//        }    </script>
原创粉丝点击