在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');
微软的网站上的说明:
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方法:
functionopenwin(url){
vara=document.createElement("a");
a.setAttribute("href",url);
a.setAttribute("target","_blank");
a.setAttribute("id","openwin");
document.body.appendChild(a);
a.click();
}
调用方式如下:
<inputtype="button"id="btn"value="百度"onclick="openwin('http://www.baidu.com');"/>
分享到:
相关推荐
对window.open进行封装, 使其更好用, 且更兼容, 很多人说window.open不兼容,其实不是, 因为不能直接执行, 必须通过用户手动触发才行;看代码: 代码如下 var openWindow = function(url, options) { var str = ""; ...
Web开发中的window.open父子窗口传值问题 部署在tomcat中进行
弹出窗口,并且弹出窗口要求最大化处理,window.open打开的窗口获取到焦点
window.open()对于各浏览器兼容情况
Window.showModalDialog 和 Window.open 用法简介 Window.showModalDialog 和 Window.open 都是 JavaScript 中的方法,用于创建新窗口或对话框,下面分别介绍它们的用法和参数。 一、Window.open() 方法 Window....
主要讲解了如何利用window.open打开新窗口,和打开新窗口时的一些参数的设置,很实用的。
扩展window.open方法,使得window.open时针对相同的URL地址非打开新窗口,而是仅仅激活已存在窗口,另外,此激活不会刷新页面,不会丢失页面上已存在的数据
Window.open ('page.html') 用于控制弹出新的窗口page.html,如果page.html不与主窗口在同一路径下,前面应写明路径,绝对路径(http://)和相对路径(../)均可。用单引号和双引号都可以,只是不要混用。 这一段代码...
window.open打开新窗口,不被拦截的方法
windows.open这个是JavaScript函数,但是在应用起来的时候总会遇到比较多的麻烦,因为参数非常多,用法也非常的多
window.open() 会打开新的浏览器窗口或 Tab,而 location.href 则是在当前页面跳转到新的 URL。 六、实际应用 在实际应用中,location.href 和 window.open() 都可以用来实现页面的跳转和弹出窗口,但是它们的用法...
使用window.open()方法发送post请求
window.open() 函数的使用中,经常会遇到一些问题,例如: * 打开的窗口无法获取焦点 * 打开的窗口无法调整大小 * 打开的窗口无法关闭 这些问题的解决方法将在后面详细介绍。 四、window.open() 函数的解决方法 ...
一、window.open()支持环境: 二、基本语法: 三、示例: 四、各项参数
关于window.open的一些特性,一些你经常不注意的,值的一看啊
NULL 博文链接:https://shawnfree.iteye.com/blog/598885
window.open()参数大全,window.open()参数大全,window.open()参数大全。
这段示例代码不但能在父窗口页面test.html中执行被打开的新窗口页面test-open.html中的代码,还可以随意调用其中的内容,甚至还可以在父窗口页面中使新窗口页面置顶(显示在最前面)。 代码已在当前的chrome , fire...
关于window.open()的详细介绍使用,浏览器对于bom的不同兼容问题
window.open使用方法以及参数说明一、window.open()支持环境: JavaScript1.0+/JScript1.0+/Nav2+/IE3+/Opera3+ 二、基本语法: window.open(pageURL,name,parameters) 其中: pageURL 为子窗口路径 name 为子窗口...