弹出窗口拦截器验证

发布于 2024-12-11 22:20:57 字数 383 浏览 0 评论 0原文

如何通过 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));

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

我该怎么做?

How can I Check if the client Browser has a popup blocker turned on via C# ?

I tried to open an popup like this

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));

But i need to open a Alert if the browser have a popup blocker

How can I do that ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安穩 2024-12-18 22:20:57

你可以这样做:

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));

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

查看其他类似问题

编辑 - 添加测试:

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>

我没觉得这有什么问题。现在,我的建议是删除 标签,并让 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);

You can do something like this:

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));

Note: Did not test whether it compiles or not, but that's the general idea.

See this other similar question

EDIT - adding test:

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"));

Produces:

<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>

I don't see anything wrong with that. Now, my suggestion is that you remove the <script></script> tags and let RegisterStarupScript add them by passing true as the last parameter as so:

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);
奈何桥上唱咆哮 2024-12-18 22:20:57

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

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

<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>

I guess all modern browsers would block popups which are not triggered by an action of the user, eg. clicking on something. Do you really need the roundtrip to the server before opening the window?

If the roundtrip is not necessary, you should do something like:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文