Jump to content
DesiLogi

jquery for button to create popup and check if it is opened before reactivating on subsequent clicks

Recommended Posts

Hello,

I have a button that opens a popup form that has a submission datapage in it. Often the user will minimize the popup because they leave the submission form open for up to a few hours (it's a timer, with a Start and a Finish time- they click 'Start' and don't submit till they later click 'Finish.'  The problem is if the popup is already open the button's code reactivates it, and also refreshes the submission datapage, losing the values that were first put in it.

I need to adjust the code so that if the user click sthe button again it needs to a) check to see if the popup is already open and, if so, un-minimize it and b) if the popup is not already open, then open it as normal.

The is code and the button are below so it's clear how it's set up- it somehow needs to have the If/Else clause fixed. Any help would be really appreciated.

<button class="shortbtnicon" type="button" onclick=" stopwatch ('https://example.com/shortcuts/stopwatch', 'MyPopUp', 450, 650);"><span><i class="fa fa-clock-o fa-1x"></i></button>

Here's the jquery to create the popup:

function stopwatch(myURL, title, myWidth, myHeight) {
  var left = (screen.width - myWidth) / 2;
  var top = (screen.height - myHeight) / 4;
  var myWindow = window.open(myURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=' + myWidth + ', height=' + myHeight + ', top=' + top + ', left=' + left);
}

It needs to be something like the below code (but this doesn't work): 

var popupurl = "https://example.com/shortcuts/stopwatch";
function stopwatch(myURL, title, myWidth, myHeight){
  //checks to see if window is open
  if(popupurl && !popupurl.closed)
  {
    popupurl.focus(); //If already Open Set focus
  }
  else
  {
        function stopwatch(myURL, title, myWidth, myHeight) {
            var left = (screen.width - myWidth) / 2;
            var top = (screen.height - myHeight) / 4;
            var myWindow = window.open(myURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=' + myWidth + ', height=' + myHeight + ', top=' + top + ', left=' + left);
         }
    stopwatch();
  }
}

 

Share this post


Link to post
Share on other sites

Got it figured out- below is the code if anyone needs this solution:

var MyPopUp = false;
function stopwatch(myURL, title, myWidth, myHeight){
  //checks to see if window is open
  if(MyPopUp && !MyPopUp.closed)
  {
    MyPopUp.focus(); //If already Open Set focus
  }
  else
  {
    var left = (screen.width - myWidth) / 2;
    var top = (screen.height - myHeight) / 4;
    MyPopUp = window.open(myURL, title,'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=' + myWidth + ', height=' + myHeight + ', top=' + top + ', left=' + left);//Open a new PopUp window
  }
}

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...