关于window.open弹出窗口被阻止的问题

原文:http://blog.csdn.net/fanfanjin/article/details/6858168

在web编程过程中,经常会遇到一些页面需要弹出窗口,但是在服务器端用window.open弹出的窗口会被IE阻止掉,showModalDialog弹出的窗口有时并不能满足我们需要,我们需要弹出新的浏览器窗口。

为什么我们编写的弹出窗口会被IE阻止呢,原来IE会自动判断弹出窗口的状态,它会阻止自动弹出的窗口,而通过我们用鼠标点击弹出的窗口,它是不会阻止的。这里就有一个问题,有人说:我的程序是写在服务器按钮里的,也是通过鼠标点击弹出的呀!其实只有在加载页面后,我们点击到弹出这段时间页面没有被重新加载的情况下,弹出的窗口才不会被阻止!这也就是说,写在服务器控件的回传事件里的window.open都会被阻止。

如果想要弹出窗口而不被阻止, 必须是用户点击之后使用window.open方可, 但是如果点击后有异步处理操作, 而且是在操作成功后再弹出, 那么这个新窗口就会被阻止了。

所以为了变通处理, 点击后就弹出一个空白的新窗口, 然后异步处理结束后再设定目标路径即可。

------------------------------------------------------------------------------------------------------------------------------

方案 1

如:

tempFunc=function(){
      var item=prodGrid.getItem(0);
      if(!item)return;
      var orderItemId=prodStore.getValue(prodGrid.getItem(0),‘purchaseOrderItemId‘);
var p=window.open(‘about:blank‘);
      var xhrArgs = {
                 url: "buyFromPreparation.action?orderItemId="+orderItemId,
                    load: function(data){
         prodStore.save();
         prodStore.url=‘getPpi.action?currentCategory1=‘+currentCategory1;
         prodStore.close();
         prodGrid._refresh();
          if(!p) alert("弹出的订单处理窗口被阻止了,请手动设置允许此窗口被打开。");
         p.location=‘checkOrder.action?orderId=‘+data;        
        },
                    error: function(error) {alert(error);}
             };     
      var d= dojo.xhrGet(xhrArgs);
     };

(先打开一个空窗口,等判断逻辑之后再 指定路径)

为什么我们编写的弹出窗口会被IE阻止呢,原来IE会自动判断弹出窗口的状态,它会阻止自动弹出的窗口,而通过我们用鼠标点击弹出的窗口,它是不会 阻止的。这里就有一个问题,有人说:我的程序是写在服务器按钮里的,也是通过鼠标点击弹出的呀!其实只有在加载页面后,我们点击到弹出这段时间页面没有被 重新加载的情况下,弹出的窗口才不会被阻止!这也就是说,写在服务器控件的回传事件里的window.open都会被阻止。

最简单有效的方法如下:
在window.open()函数中增加一个参数,将target设置为‘self’,
即改为使用: window.open(link,‘_self‘);

微软的网站上的说明:http://technet.microsoft.com/zh-cn/library/cc766478(v=WS.10).aspx

Pop-Up Blocking
The Pop-up Blocking feature blocks pop-up (and pop-under) windows initiated automatically by a Web site. Internet Explorer blocks Pop-up windows in the Internet and Restricted sites zones by default. However, the Pop-up Blocker enables pop-up windows initiated by a user action. Users can configure Internet Explorer 6 for Windows XP with SP2 to be more or less restrictive. Users can also turn off the Pop-up Blocker altogether. Generally, the Pop-up Blocker enables a window to open under the following circumstances:

? When initiated by user action, such as clicking a button or hyperlink

? When opened in the Trusted sites and Local intranet zones (considered safe)

? When opened by other applications running on the local computer

The affected script methods are:

window.open
window.showHelp
window.showModalDialog
window.showModelessDialog
window.external
window.NavigateAndFind
注:
Pop-ups created with window.createPopup are unaffected by the Pop-up Blocker.

------------------------------------------------------------------------------------------------------------------------------------------------------------

方案 2

由于在使用window.open时,在很多情况下,弹出的窗口会被浏览器阻止,但若是使用a链接target=‘_blank‘,则不会,基于这一特点,自己封装了一个open方法:

function openwin(url) {
var a = document.createElement("a");
    a.setAttribute("href", url);
    a.setAttribute("target", "_blank");
    a.setAttribute("id", "openwin");
    document.body.appendChild(a);
    a.click();
}

调用方式如下:

<input type="button" id="btn" value="百度" onclick="openwin(‘http://www.baidu.com‘);" />

关于window.open弹出窗口被阻止的问题

时间: 2024-08-23 01:39:48

关于window.open弹出窗口被阻止的问题的相关文章

js window.open()弹出窗口参数说明及居中设置

window.open()可以弹出一个新的窗口,并且通过参数控制窗口的各项属性. 最基本的弹出窗口代码 window.open('httP://codeo.cn/'); window.open()各参数详解 示例代码: window.open('httP://codeo.cn/', 'newwindow', 'height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no, loc

window open 弹出窗口 和 同一窗口下打开新标签页

<!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title></title> </head> <body> <a href="https://www.baidu.com/" class ="popup">

window.open弹出窗口调用controller

前台图片调用js函数 <img src='${pageContext.request.contextPath}/FlatUI/img/link.png' id='report' alt='"+data[i].report+"' onclick='changeUrl(this,"+data[i].id+")' width=15px height=15px/> js函数, 其中有一项重要处理, 在open窗口关闭的同时, 当前窗口刷新 function ch

弹出窗口

1.新建模式对话框(浏览器窗口在最顶层): 1 function DotVideo2() { 2 var id = '@ViewBag.TopicsID'; 3 //模式对话框 4 window.showModelessDialog('/ConferenceFront/DocVideoPlay?TopicID=' + id + '', 'newwindow', 'dialogheight:480px;dialogwidth:300px;dialogtop:10px;dialogleft:880p

window.open() 某些情况会被浏览器阻止弹出窗口及解决办法

window.open() 的作用是创建一个新的浏览器窗口用来打开相关的资源,这是一个原生的 Javascript API 接口. 有关 window.open() 的基本使用可以参考 mozilla 提供的  API 文档:window.open . 大部分现代的浏览器(泛指 Chrome / Firefox / IE 10+ / Safari)都默认开启了阻止弹出窗口的策略,原因是 window.open 被广告商滥用,严重影响用户的使用.这个阻止弹出窗口的操作,并不是直接封杀 windw.

Js弹出窗口代码,window.open方法

<html><script language=JavaScript> <!--function click() {if (event.button==2) {window.open('#','popwindows',"toolbar=no,menubar=no,width=200,height=200")}}document.onmousedown=click//--></script><head><meta http-

(转)弹出窗口lhgDialog API文档

应用到你的项目 如果您使用独立版本的lhgDialog窗口组件,您只需在页面head中引入lhgcore.lhgdialog.min.js文件,4.1.1+版本做了修改可以和jQuerya库同时引用,而且4.1.1+版本的独立组件的lhgcore库做了极大的修改,专门为组件定制,压缩后才6K与组件合在一起总大小才不到20K,效率上得到很大提高,比引用jQuery快很多,但这里要注意如果你同时引用了jQuery库的话必须把$换成J,如果没引用jQuery库则可直接使用$. <script type

Web开发技巧:使用自定义数据属性创建弹出窗口

在开发web应用时,有时会用JavaScript获取文档之外的信息,某些情况下,我们需要用一些技巧来处理这些额外信息以保证Web应用能够正常运行.一般而言,技巧无外乎是将额外的信息塞入事件处理程序或滥用rel属性或class属性以方便注入行为.感谢HTML规范引入了自定义数据属性,让不堪回首的往日一去不返. 所有的自定义数据属性都以data一前缀开头,HTML文档的验证器会在验证时忽略它.开发人员可以在任意元素中加入自定义数据属性,属性值可以是照片的元数据.经纬度坐标或者弹出窗口的尺寸.最棒的是

jsp网页弹出窗口的多种办法

http://blog.csdn.net/huangfoxjava/article/details/2973033 登录|注册     huangfoxjava的专栏 目录视图 摘要视图 订阅 关于jsp网页弹出窗口[很多种方法......] 分类: JSP2008-09-24 14:22 5799人阅读 评论(0) 收藏 举报 jspjavascript框架html脚本浏览器 各种弹出页面的设计 [1.普通的弹出窗口] 其实代码非常简单: <SCRIPT LANGUAGE=javascript