window.open在IE下会被IE阻 window.open(url,name) name参数值有空

异步请求时IE出错的问题,解决方法:

先把数据导入到Execl,导入成功后在下载,用异步的方法,

exportUrl是导入excel请求的地址,downloadUrl是下载的地址

注意要发送两次请求,因为IE把第一次请求给阻止了

//根据传入的名字导出Excel
function exportExcel(exportUrl,downloadUrl){
	var p=window.open(downloadUrl,"_blank");
	$.ajax({
		type:'post',
		url: exportUrl,
		success:function(){
			if(!p)alert("弹出的订单处理窗口被阻止了,请手动设置允许此窗口被打开。"); 
			p.location.href=downloadUrl
		},
		error: function(error) {$.messager.alert('抱歉','请重新下载');  }
	});
}





原文地址http://www.camnpr.com/archives/window-open-ie-block-and-special-char.html

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

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

 

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

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

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

方案 1

var func=function(){
      var p=window.open('about:blank');
      $.ajax({
          type:"post",
          url:"http://www.camnpr.com/getData.action",
          dataType:"json",
          success:function(data){
              if(!p)alert("弹出的订单处理窗口被阻止了,请手动设置允许此窗口被打开。");
              p.location='kuabaobao.action?log='+data; //关键点
            },
            error: function(error) {alert(error);}
      }; 
     };

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

最简单有效的方法如下:
在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方法:

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

调用方式如下:

< input  type ="button"  id ="btn"  value ="郑州网建"  onclick ="openwin('http://www.camnpr.com');"   />

 window.open(url,name) name参数值有空格、符号问题

先看代码:

function search(keywords){
window.open("http://www.camnpr.com/search?key="+escape(keywords), "关键词:”"+keywords+"“ 的搜索结果");
}

错误: SCRIPT87: 参数无效。 
camnpr.js, 行8 字符2

这是什么问题? 参数不对吗? 反复看了几遍,无奈,用排除法,先用:window.open(argument1),结果正常。那就定位到,第二个参数错误了,可以,会哪里有错呢? 还是排除法,window.open(url,"camnpr"); 结果正常。噢,找到问题的所在了,于是,查询了一个window.open的使用方法:

w3school的解释如下:

window.open(URL,name,features,replace)

name: 一个可选的字符串,该字符串是一个由逗号分隔的特征列表,其中包括数字、字母和下划线,该字符声明了新窗口的名称。这个名称可以用作标记 <a> 和 <form> 的属性 target 的值。如果该参数指定了一个已经存在的窗口,那么 open() 方法就不再创建一个新窗口,而只是返回对指定窗口的引用。在这种情况下,features 将被忽略。

 

根据上面open的说明,可以知道name实际上是一个变量,指定的是新窗口的名称,而不是新窗口的title。额,以前一直以为是新窗口的Title。希望看到这个博文的人也注意下这个小细节问题。

测试一下

Window.open方法测试(请在IE下哦,亲)



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值