1、 在web编程过程中,经常会遇到一些页面需要弹出窗口,但是在服务器端用window.open弹出的窗口会被IE阻止掉,
showModalDialog
弹出的窗口有时并不能满足我们需要,我们需要弹出新的浏览器窗口。
2、
为什么我们编写的弹出窗口会被
IE
阻止呢,原来
IE
会自动判断弹出窗口的状态,它会阻止自动弹出的窗口,而通过我们用鼠标点击弹出的窗口,它是不会阻止的。这里就有一个问题,有人说:我的程序是写在服务器按钮里的,也是通过鼠标点击弹出的呀!其实只有在加载页面后,我们点击到弹出这段时间页面没有被重新加载的情况下,弹出的窗口才不会被阻止!这也就是说,写在服务器控件的回传事件里的
window.open
都会被阻止。
3、
问题搞清楚了事情也就好办了,我们可以用
ajax
来与服务器端通信,在客户端用
javascript
来编写事件处理程序,这样在客户端的就不会因为回传事件而重新加载页面,这样弹出窗口就不会被阻止了!
举个例子:
default.aspx
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>

<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
runat
="server"
>
<
title
>
无标题页
</
title
>
</
head
>

<
script
language
="javascript"
>
...

function getAjaxXml()
...{
var xml = new ActiveXObject("Microsoft.XMLHTTP");
var str="flag=888";
xml.open("GET","showxml.aspx?"+str,false);
xml.send();
return xml.responseText;
}
function imgclick()
...{
var value=getAjaxXml();
window.open ("http://www.a8.com/?xml="+value );
}
</
script
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
div
>
<
img
src
= "http://img.a8.com/em/mlblog02.gif"
style
=" cursor :hand"
onclick
="imgclick()"
/>
</
div
>

</
form
>
</
body
>
</
html
>
showxml.aspx.cs
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
namespace
webtest
...
{
public partial class showxml : System.Web.UI.Page
...{
protected void Page_Load(object sender, EventArgs e)
...{
Response.Write("xml"+Request ["flag"]);
}
}
}
本文介绍了解决Internet Explorer浏览器自动阻止网页弹出窗口的方法。通过使用Ajax技术进行服务器通信,并利用JavaScript处理客户端事件,避免因页面刷新而导致的弹窗被阻止的问题。
1739





