diff --git a/README.md b/README.md index a1b862c..f0bac9d 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,7 @@ There are more [examples] to look at as well. | `toolbar` | boolean | `false` | | | `top` | integer | `0` | | | `width` | integer | `500` | | +| `forcerefresh`|boolean | `true` | if popup is still open it will refresh the window. If the user clicked a link and was taken to a different page, they will be taken back to the original page. Likewise, if the user has entered text on a form or performed some action that needed saved, they will lose any unsaved work. ## License diff --git a/jquery.popupwindow.js b/jquery.popupwindow.js index 79ed5a6..8f7fb80 100644 --- a/jquery.popupwindow.js +++ b/jquery.popupwindow.js @@ -18,7 +18,8 @@ status: false, toolbar: false, top: 0, - width: 500 + width: 500, + forcerefresh:true }; $.popupWindow = function(url, opts) { @@ -45,15 +46,43 @@ params.push('left=' + options.left); params.push('top=' + options.top); - // open window + // define name var random = new Date().getTime(); var name = options.name || (options.createNew ? 'popup_window_' + random : 'popup_window'); - var win = window.open(url, name, params.join(',')); + + // setup list of handles if needed, and check if win handle exists + var winPOS = -1; + if (!$.popupWindow.win) { $.popupWindow.win = []; } + for (var i = 0, len = $.popupWindow.win.length; i < len; i++) { + if ($.popupWindow.win[i].name === name) { winPOS = i; break; } + } + + // add to list of handles if it didnt already exist + var winHref; + if (winPOS === -1) { + $.popupWindow.win.push({ name: name, win: { closed: true } }); + winPOS = $.popupWindow.win.length - 1; + + // try regaining access to the handle, if user refreshed but didnt close all popups + // this will fail if its of a different origin + $.popupWindow.win[winPOS].win = window.open("", name, params.join(',')); + try { winHref = $.popupWindow.win[winPOS].win.location.href; } catch (e) { } + } + + // determine whether to open window + // the user wants to always refresh, the handle says its closed, the href is a new page + // winHref could be 3 possible values + // 1. undefined - this failed the assignment above, due to different origin - dont reload url + // 2. some url - this passed the assignment above, must be same origin - dont reload url + // 3. about:blank - the window didnt exist and the user now has a blank window - reload url + if (options.forcerefresh || $.popupWindow.win[winPOS].win.closed || winHref === "about:blank") { + $.popupWindow.win[winPOS].win = window.open(url, name, params.join(',')); + } // unload handler if (options.onUnload && typeof options.onUnload === 'function') { - var unloadInterval = setInterval(function() { - if (!win || win.closed) { + var unloadInterval = setInterval(function () { + if (!$.popupWindow.win[winPOS].win || $.popupWindow.win[winPOS].win.closed) { clearInterval(unloadInterval); options.onUnload(); } @@ -61,11 +90,11 @@ } // focus window - if (win && win.focus) { - win.focus(); - } + if ($.popupWindow.win[winPOS].win && $.popupWindow.win[winPOS].win.focus) { + $.popupWindow.win[winPOS].win.focus(); + } // return handle to window - return win; + return $.popupWindow.win[winPOS].win; }; })(jQuery);