Lucas_Santos
Lucas_Santos

Reputation: 4740

PopUp Blocker verify

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 ?

Upvotes: 0

Views: 5752

Answers (2)

Magnus
Magnus

Reputation: 1022

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>

Upvotes: 0

Icarus
Icarus

Reputation: 63972

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

Upvotes: 5

Related Questions