解決JS彈出新視窗被瀏覽器阻止的解決方案

日出曙光發表於2016-09-07
本文轉載自:http://blog.csdn.net/cntanghai/article/details/6643522
在js中通過open彈出視窗可能會被阻止,我嘗試這段程式碼:  
          var flag = window.open(url,"","");  
          if(flag==null) {  
             alert("您的瀏覽器啟用彈出視窗過濾功能!\n\n請暫時先關閉此功能!") ;  
          }   
  
上面這段程式碼僅僅是增加了一個提示,讓使用者更改瀏覽器設定.但實際上無任何作用.使用下面的解決方案後,在IE中測試,一般不會被阻止,即使被阻止,ie也會彈出讓使用者選擇是否<pre name="code" class="html">1.新新增一個Form  
  
<form id="EmailForm" action="目標頁" method="get" target="_blank">  
<input id="emailid" name="emailid"  type="hidden" />//隱藏域,用來放需要傳遞的引數  
</form>  
  
注意form的target屬性要設定成_blank  
  
2.提交這個Form  
  
function btnEditEmail_onclick() {  
document.getElementById("emailid").value=id;//初始隱藏域  
document.getElementById("EmailForm").submit();//提交  
}  
  
這樣,就會開啟新視窗定向到目標頁了,而且不會被阻止,同時用新視窗的window.opener還能訪問原視窗。  
本段內容引自:http://hi.baidu.com/hy0kl/blog/item/486c01f31882e55c352acc1a.html  

允許彈出視窗.所以說這種解決方案是行之有效的.
我的程式碼:  
<%@ page language="java" pageEncoding="utf-8"%>  
<%@ include file="/summer/component/common/sys.jsp"%>  
<%@ include file="/summer/jsp/head/taglibs.jsp"%>  
<html>  
<head>  
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
    <link href="<c:url value='/css/css.css'/>" rel="stylesheet" type="text/css" />  
    <title>密碼確認</title>  
<script type="text/javascript">  
  
function tijiao(){ // 提交  
    var password = document.getElementById("password").value;  
    var map = new Map();  
    map.put('key', 'maintain.checkPassWord');  
    map.put('password', password);  
    var query = new QueryObj(map, chakanHeTongjiCallback);  
    query.send();   
}  
function chakanHeTongjiCallback(query) {  
    if(query.getDetail()=='false'){  
         alert('密碼輸入錯誤,請重試!');  
         return;  
    }  
    var type = document.getElementById("type").value;  
    var cpbbh = document.getElementById("cpbbh").value;  
    var plx = document.getElementById("plx").value;  
    if(type=='1') { // 投票詳情  
        toupiao(cpbbh,plx);  
    }else if(type=='2') { //統計  
        var submitForm =  document.getElementById("tongjiForm");  
        submitForm.submit();  
        window.close();  
    }     
 }  
   
 function toupiao(cpbbh,plx){  
    var map = new Map();  
    map.put('key', 'maintain.judge');  
    map.put('cpbbh', cpbbh);  
    map.put('plx', plx);      
    var query = new QueryObj(map, toupiaoCallback);  
    query.send();  
   
 }  
   
 function toupiaoCallback(query) {  
    if(query.getDetail()=='false'){  
        alert('該測評表沒有任何參與投票資訊');  
        return;  
    }  
    var submitForm =  document.getElementById("toupiaoxiangqingForm");  
    submitForm.submit();  
    window.close();  
   
 }  
  
  
</script>  
</head>  
  
<body>  
<center>  
  
<div>  
 <div width="390"> </div>  
 <div width="390">請輸入您的密碼:</div>  
 <table>  
  <tr><td><input type="password" id="password" name="userPasWord" /></td></tr>  
 </table>  
</div>  
<br>  
<br>  
<div>  
 <input name="submit" type="submit" onclick="tijiao()" value="提交" class="anniu6" />  
             
 <input name="submit" type="button" onclick="window.close();" value="關閉" class="anniu6" />  
</div>  
<ui:hidden name="type" value="${type }"></ui:hidden>  
<ui:hidden name="cpbbh" value="${cpbbh }"></ui:hidden>  
<ui:hidden name="plx" value="${plx }"></ui:hidden>  
</center>  
  
<%-- 統計 --%>  
<form action="../mzcp/voteDetail.do?action=statisticsVoteDetail" id="tongjiForm" method="post"  target="_blank">  
    <ui:hidden name="NCpbbh" value="${cpbbh }"></ui:hidden>  
    <ui:hidden name="NPlx" value="${plx }"></ui:hidden>  
</form>  
<%--投票詳情 --%>  
<form action="../mzcp/voteDetail.do?action=singleVoteDetail" id="toupiaoxiangqingForm" method="post"  target="_blank">  
    <ui:hidden name="NCpbbh" value="${cpbbh }"></ui:hidden>  
    <ui:hidden name="NPlx" value="${plx }"></ui:hidden>  
    <ui:hidden name="index" value="0"></ui:hidden>  
</form>  
</body>  
  
</html>  





相關文章