PopUp Blocker验证

时间:2011-10-26 15:47:20

标签: c# javascript jquery asp.net .net

如何检查客户端浏览器是否通过C#打开了弹出窗口阻止程序?

我试图像这样打开一个弹出窗口

ClientScript.RegisterStartupScript(this.GetType(), "newWindow", String.Format("<script>window.open('{0}', 'Cliente', 'toolbar=no,directories=no,status=no,menubar=no, scrollbars=yes,resizable=no', '720', '600', 'true'); </script>", url));

但如果浏览器有弹出窗口阻止程序,我需要打开警报

我该怎么做?

2 个答案:

答案 0 :(得分:5)

您可以这样做:

ClientScript.RegisterStartupScript(this.GetType(), "newWindow", String.Format("<script>var myPopup = window.open('{0}', 'Cliente','toolbar=no,directories=no,status=no,menubar=no, scrollbars=yes,resizable=no','720', '600', 'true');if(!myPopup)alert('a popup was blocked. please make an exception for this site in your popup blocker and try again');</script>",url));

注意:没有测试它是否编译,但这是一般的想法。

See this other similar question

编辑添加测试:

string mys="<script>var myPopup = window.open('{0}', 'Cliente','toolbar=no,directories=no,status=no,menubar=no, scrollbars=yes,resizable=no','720', '600', 'true');if(!myPopup)alert('a popup was blocked. please make an exception for this site in your popup blocker and try again');</script>";

Console.WriteLine(string.Format(mys,"page.aspx"));

制作:

<script>var myPopup = window.open('page.aspx', 'Cliente','toolbar=no,directories=no,status=no,menubar=no, scrollbars=yes,resizable=no','720', '600', 'true');if(!myPopup)alert('a popup was blocked. please make an exception for this site in your popup blocker and try again');</script>

我没有看到任何错误。现在,我的建议是删除<script></script>标记,让RegisterStarupScript通过传递true作为最后一个参数来添加它们:

ClientScript.RegisterStartupScript(this.GetType(), "newWindow", String.Format("var myPopup = window.open('{0}', 'Cliente','toolbar=no,directories=no,status=no,menubar=no, scrollbars=yes,resizable=no','720', '600', 'true');if(!myPopup)alert('a popup was blocked. please make an exception for this site in your popup blocker and try again');",url),true);

答案 1 :(得分:0)

我猜所有现代浏览器都会阻止不是由用户的动作触发的弹出窗口,例如。点击某事。在打开窗口之前,你真的需要往服务器的往返吗?

如果不需要往返,您应该执行以下操作:

<input type="button" onclick="openWindow()" value="open window" />
<script type="text/javascript">
    function openWindow() {
        window.open('<%= Url %>', 'Cliente', 'toolbar=no,directories=no,status=no,menubar=no, scrollbars=yes,resizable=no', '720', '600', 'true');
    }
</script>