1

I open a popup with the click event of a hyperlink... The popup contains records from a server.

The problem is that when I click rapidly, there are multiple popups at once. There is a way to prevent this? in which can open a single popup

My code:

$('.wrapper_form a.add').click(function(e)
{
    e.preventDefault();

    if(typeof(currentPopup) == 'undefined' || currentPopup.closed)
    {
        url = 'server_page.aspx';
        currentPopup = window.open(url,'server','height=500,width=800');
        if (window.focus) {currentPopup.focus()}
    }
    else
    {
        currentPopup.focus();
    }
});
casperOne
  • 73,706
  • 19
  • 184
  • 253
csotelo
  • 1,453
  • 2
  • 28
  • 43

1 Answers1

4

Here is one approach. Not the best solution but it should work. What this code will do is protect against clicking the link a bunch of times and have it open a new instance for each click. This code will not allow the window to be opened more than once in a 1/2 interval, of course you can change the timing.

var hopefullyThisIsNotInGlobalScope = false;

$('.wrapper_form a.add').click(function(e)
{
if (hopefullyThisIsNotInGlobalScope)
{
 return false;
}
    hopefullyThisIsNotInGlobalScope = true; 
    setTimeout(function () { hopefullyThisIsNotInGlobalScope = false; }, 500);
    e.preventDefault();

    if(typeof(currentPopup) == 'undefined' || currentPopup.closed)
    {
        url = 'server_page.aspx';
        currentPopup = window.open(url,'server','height=500,width=800');
        if (window.focus) {currentPopup.focus()}
    }
    else
    {
        currentPopup.focus();
    }
});

Assuming the popup is on the same domain as the window launching it you might be able to replace hopefullyThisIsNotInGlobalScope variable with a global var attached to the window. You can then set that variable when the popup launches and alter it using the browser unload event

Community
  • 1
  • 1
j-u-s-t-i-n
  • 1,402
  • 1
  • 10
  • 16