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.

1 Answer 1

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

2
  • 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
    Commented Sep 2, 2015 at 18:10
  • 1
    @pmvsdt - That is a different question that what you originally posted. Please start a new question to troubleshoot that
    – Eric
    Commented Sep 2, 2015 at 18:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .