-1

right now I'm trying to open a new popup window using JavaScript but I just want a single popup window to be opened if you click several times a link, but if I close the window I want it to open to popup again.

So far I have thw following:

.js

function open() {
    var popup = window.open("http://www.google.com/", "URL","left=0,top=0,width=800,height=600,status=no,resizable=no,scrollbars=no,toolbar=no,menubar=no");
}

.html

<a onclick="javascript:open()">Click me</a>

It works nice using Chrome and FF, but when I test it with IE it opens a new window for every click.

I would like to know if there is a way to fix this.

Patrick
  • 17,669
  • 6
  • 70
  • 85
Tomarto
  • 2,755
  • 6
  • 27
  • 37

4 Answers4

0
function open() {
    document.getElementById('link').onclick = '';
    // open popup
}

<a id="link" onclick="javascript:open()">Click me</a>

Note, this is not a thorough solution, and does not account for situations where the popup window is blocked.

Brad M
  • 7,857
  • 1
  • 23
  • 40
  • My bad, I didn't specified I want to be able to reopen the popup if I close the popup window and click the link again. I just edited the question. Thanks! – Tomarto Mar 13 '13 at 16:07
0

Use a variable to check if it's been opened before.

var opened = false;
function open() {
    if (!opened) {
        var popup = window.open("http://www.google.com/", "URL","left=0,top=0,width=800,height=600,status=no,resizable=no,scrollbars=no,toolbar=no,menubar=no");
        popup.onload = function() {
            popup.onunload = function() {
                opened = false;
            };
        };
        opened = true;
    }
}

You can read this for the nested callback explanation.

Why does window.open(...).onunload = function () { ... } not work as I expect?

Community
  • 1
  • 1
GJK
  • 37,023
  • 8
  • 55
  • 74
  • My bad, I didn't specified I want to be able to reopen the popup if I close the popup window and click the link again. I just edited the question. Thanks! – Tomarto Mar 13 '13 at 16:07
0

@Tomarto

Try this code ,

function Redirect (url, name, args)
 {
   if (typeof (popupWind) != "object" || popupWind.closed) 
   {
       popupWind= window.open(url, name, args); 
   }

   popupWind.focus(); 
   return popupWind;
 }
alj
  • 1
0

To open only one instance of a popup window in an HTML page, use the "windowName" parameter of the window.open method. For eg: window.open("http://www.mywebsite.com") will open a new window each time the user clicks the link containing the window.open code. To open in same window use, window.open("http://www.mywebsite.com","contacts") which will therefore open only one instance of the window for any "N" number of clicks on the links.