[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 369
  • Last Modified:

avoid popup blockers via an input button instead of a asp button

Even though some people disagree, there are certain occasions where popups are the way to go, at least that is my opinion. On that note I need help with the following problem.

I found that if I use an input button (not an asp button) with an attached onclick javascript function to “window.open” a page in a new window, most popup blockers will allow the page to open. That includes IE, Safari, Chrome, FireFox. On the other hand  Opera will advise that a pop was blocked which is good enough.

This is the problem, if I pass a URL as a string to the window.open function in javascript, it will work but I need to procees in codebehind the selections made via checkboxes and create the string on demand. To get the function in codebehind to work I create a  __doPostBack to the specific function and transfer the string that results from the selection to a hidden variable on the aspx to be able to access the info on javascript and feed it to the window.open function.
The result from the codebehind processing works but does not get back to the javascript on time. If I press the button again then it does work. So basically I end up with two new windows, one bad and one good.

If there is a different solution that will give me the same results I will accept that. Please, if posible provide the solution in vb.net.
Thank  you for your help.

<script type="text/javascript" language ="javascript" >
function myPopup() 
  var jsVar ='';  
  __doPostBack('GetMapURL', jsVar);
  var jsVar2 = document.form1.hiddenURL.value; 
  /*also tried jsVar2 = '<%=mapcoordinate%>'; by placing the info on a public variable */
 /*in both cases only after hitting the button for a second time does jsVar2 fillup*/
  var jsVar1 = 'http:\/\/www.bing.com/maps/?v=';


<asp:HiddenField ID="hiddenURL"  runat="server"  />

<asp checkbox> many of them

<input id="getmapbutton" type="button" onclick="myPopup()" value="POP!"/>

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  If IsPostBack Then
    Me.ClientScript.GetPostBackEventReference(Me, "arg")
    Dim eventTarget As String = Me.Request("__EVENTTARGET")
    Dim eventArgument As String = Me.Request("__EVENTARGUMENT")
    If eventTarget <> String.Empty AndAlso eventTarget = "GetMapURL" Then
    End If
  End If

'continues the processing

'in the same file
Public Sub GetMapFunction()
   hiddenURL.Value = .....
End Sub

Open in new window

  • 4
  • 4
1 Solution
IvicarpAuthor Commented:
In the java script it reads

it should read
From my understanding you like to open popup based on some server side calculation.

To open popup you can use below server side code.

Dim strURL As String

Page.ClientScript.RegisterStartupScript(Page.GetType(), "popup", "<script language=JavaScript>window.open('" + strURL + "');</script>")

Open in new window

IvicarpAuthor Commented:
Rajapandian 81: Yes, you are correct with your statement but the solution proposed gets blocked, now also IE and firefox show a message but Chrome and Safari dont even say anything and just refresh the page.
I have tried a lot of things, what apparently work is when the window.open is called by a ref or input button as javascript in the aspx file (client processing) if you refence it or any other way or place, that I found so far, it gets blocked.
The problem the way I see it is that the __doPostBack('GetMapURL', jsVar); gets the process started in the code behind but does not wait to get the codebehind finished to move to the next line in the javascript. (which makes sense) and that is why probably I need to press the button twice and end up getting two windows.
Is there a way from the input button (not aspx button) to start via client javascript a function in code behind that after processing the checkboxes will return a value which I can then feed it to the window.open in the client script?
I may have changed the question, I apologize if I did that, but the interaction is what makes me make better questions.
Thanks again
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

May I know what type of calculation you are donig in server side.

You can do everything in client side. If you want to connect databse you can use XMLHTTP.
IvicarpAuthor Commented:
rajapandian 81: I verify 4 checkboxes, if marked any of them, I take the associated string to each checkbox plus a string from a textbox and create a url that includes a set of addresses to get a bing.com/maps. Theoretically I could probably verify the checkboxes on the client side and get the string from the textbox and do the creation of the final string on the client side with javascript and then call window.open. Making sure I never have a postback. Currently I have the creation of the string in code behind. As soon as I have somewhere a postback the window.open gets blocked by the popup blocker which I want to avoid. Safari does not even warn that your window is been blocked. The user has no idea of what is going on. A real pain.

To get TextBox value:
var txtValue = document.getElementById('<%= TextBox1.ClientID %>').value;

To get CheckBox text:
var chkboxValue;
var chkbox = document.getElementById('<%= CheckBox1.ClientID %>');
var lbl = chkbox.nextSibling;
       lbl = lbl.nextSibling;
chkboxValue = lbl.innerHTML;

To check CheckBox checked or not (return true false):
var chkbox = document.getElementById('<%= CheckBox1.ClientID %>');
var chkboxchecked = chkbox.checked;
IvicarpAuthor Commented:
rajapandian 81:
Yup, i had to convert the entire function to client side javascript but it works the way I wanted.
Thanks for your patience and help.
Glad to help. :)

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now