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

来源:互联网 发布:经期减肥知乎 编辑:程序博客网 时间:2024/04/30 01:25
 

现在类似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>

上面的没有测试过

下面自己写的:

  1. <script language="javascript">   
  2.   
  3.         //    检测   
  4.   
  5.         var openWin = window.open("Login.aspx","信息系统","location=no,resizable=yes,status=yes,menubar=no,center=yes,scrollbars=no,left=0,top=0");   
  6.   
  7.         if ( openWin != null )   
  8.   
  9.         {   
  10.   
  11.             openWin .resizeTo(screen.availWidth,screen.availHeight);   
  12.   
  13.             window.opener = 'X';   
  14.   
  15.             window.open('','_parent','');   
  16.   
  17.             window.close();   
  18.   
  19.   
  20.   
  21.         }   
  22.   
  23.         else  
  24.   
  25.         {   
  26.   
  27.             alert('对不起,请关闭阻止弹出窗口程序,或者将本网站加入到受信任站点!');   
  28.   
  29. //            var host = top.location.host;   
  30.   
  31. //            var path = top.location.pathname;   
  32.   
  33. //            path = path.substr(1,path.length);   
  34.   
  35. //            path = path.substr(0,path.indexOf('/'));   
  36.   
  37. //            var url = 'http://' + host + '/' + path + '/Default/LogOut.aspx';   
  38.   
  39. //            top.location.href=url;   
  40.   
  41.         }   
  42.   
  43. //        if ( top.window.name != "信息系统" )   
  44.   
  45. //        {   
  46.   
  47. //            window.opener = 'X';   
  48.   
  49. //            window.open('','_parent','');   
  50.   
  51. //            window.close();   
  52.   
  53. //        }   
  54.   
  55.     </script>  
原创粉丝点击