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 ?
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 ?
Share Improve this question asked Oct 26, 2011 at 15:47 Lucas_SantosLucas_Santos 4,74018 gold badges74 silver badges121 bronze badges 5- So you want to fix a blocked popup with another popup? Perhaps the user doesn't want any popups. – JaredPar Commented Oct 26, 2011 at 15:48
- @JaredPar alert windows are not blocked by pop-up blockers. – Icarus Commented Oct 26, 2011 at 15:52
- I want open a popup. If the browser has the PopUp Blocker turned on, I just sent a Alert Message – Lucas_Santos Commented Oct 26, 2011 at 15:52
- 1 I'd help but pop-ups are horrible. Also as C# is server-side I don't think it will be giving you much information on the client's browser. – Jack Commented Oct 26, 2011 at 15:55
- @Icarus yes I realize they're not blocked by pop-up blockers. They still do "pop-up" though. – JaredPar Commented Oct 26, 2011 at 15:56
2 Answers
Reset to default 5You 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 piles 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);
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>