0

Have a custom button and trying to open external link on button click. It works fine. But I would like to check if the window is already open and if yes, donot open another window:

right now i have (on button click) > Execute Javascript : enter image description here

window.open('http://www.google.com');

I tried to add function to check if window is already open as below:

var isOpen = "false";
alert('hi');
function OpenPopup()
{
   alert('hi');
   if(isOpen == "false")
   {
         isOpen = "true"; 
         window.open('http://www.google.com');
    }
}

But seems like I cannot use function inside as the onclick is not working after changing the code. First alert works but the one inside function is not working.

3

If you give the window a name when opening it initially then it will not open a new window:

window.open('abc.html','my_window_name');

However it will refresh the window...

This works as you would expect and does not reopen the window:

<apex:page >

<script>
var isOpen = false;

function OpenPopup()
{

   if(isOpen == false)
   {
         alert('Opening');
         isOpen = true; 
         window.open('http://www.google.com','myGoogle');
    }else{
       alert('Already Opened');
    }
}
</script>

<button onclick="OpenPopup(); return false;">Click me</button>

</apex:page>

Having the name in the window.open ensure that if the refresh the VF page and click it again, it will just refresh the previously opened window instead of opening a new one

  • Thanks Eric. The first solution, window.open with name is working fine for all websites but for some reason, it is not working on our internal web address .. – sf_user Sep 2 '15 at 18:10
  • 1
    @pmvsdt - That is a different question that what you originally posted. Please start a new question to troubleshoot that – Eric Sep 2 '15 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.