From 91c3f3f427079952b142ae02b2006a6f65effd12 Mon Sep 17 00:00:00 2001 From: Todd Horst Date: Fri, 18 Apr 2014 12:58:09 -0400 Subject: [PATCH 1/7] Added forcerefresh this will allow users to define if they want to refresh the window should it still exist. This requires them to pass a name for that window, and toggle force refresh off. if they dont want to refresh it will simply bring it in focus --- jquery.popupwindow.js | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/jquery.popupwindow.js b/jquery.popupwindow.js index 79ed5a6..da6ca81 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,33 @@ 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(',')); + + // get existing win handle if exists + // would be nice if we could make this persistent + if (!$.popupWindow.win) { $.popupWindow.win = []; } + var winPOS = -1; + for (var i = 0, len = $.popupWindow.win.length; i < len; i++) { + if ($.popupWindow.win[i].name === name) { winPOS = i; break; } + } + + // add to global if it didnt already exist + if (winPOS === -1) { + $.popupWindow.win.push({ name: name, win: { closed: true } }); + winPOS = $.popupWindow.win.length - 1; + } + + // determine whether to open window + if (options.forcerefresh || $.popupWindow.win[winPOS].win.closed) { + $.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 +80,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); From 4fba1fe605739cdbdc4692792b1cfd7fb6f6ab32 Mon Sep 17 00:00:00 2001 From: Todd Horst Date: Fri, 18 Apr 2014 12:58:51 -0400 Subject: [PATCH 2/7] Added documentation for forcerefresh --- README.md | 1 + 1 file changed, 1 insertion(+) 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 From 460096e686a5edf8b9022b3f6f0bd8202f42e1c3 Mon Sep 17 00:00:00 2001 From: Todd Horst Date: Sat, 19 Apr 2014 07:45:10 -0400 Subject: [PATCH 3/7] Fix to allow persistence I tried using localstorage api, but you cannot store the object and retrieve it. Maybe IndexedDB would work, but this hack works just as well at grabbing a refrence, even if the parent page was refreshed. --- jquery.popupwindow.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/jquery.popupwindow.js b/jquery.popupwindow.js index da6ca81..3be69cd 100644 --- a/jquery.popupwindow.js +++ b/jquery.popupwindow.js @@ -51,7 +51,6 @@ var name = options.name || (options.createNew ? 'popup_window_' + random : 'popup_window'); // get existing win handle if exists - // would be nice if we could make this persistent if (!$.popupWindow.win) { $.popupWindow.win = []; } var winPOS = -1; for (var i = 0, len = $.popupWindow.win.length; i < len; i++) { @@ -59,13 +58,19 @@ } // add to global 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 + $.popupWindow.win[winPOS].win = window.open("", name, paramsJoin); + try { winHref = $.popupWindow.win[winPOS].win.location.href; } catch (e) { } } // determine whether to open window - if (options.forcerefresh || $.popupWindow.win[winPOS].win.closed) { + // the user wants to always refresh, the handle says its closed, the href is a new page + if (options.forcerefresh || $.popupWindow.win[winPOS].win.closed || winHref === "about:blank") { $.popupWindow.win[winPOS].win = window.open(url, name, params.join(',')); } From b905903b6f3472cc6a9a69d967caa301404f20d8 Mon Sep 17 00:00:00 2001 From: Todd Horst Date: Sat, 19 Apr 2014 12:35:43 -0400 Subject: [PATCH 4/7] fix href check I think its smarter to check if its not undefined. I dont know if its standard to be `about:blank` --- jquery.popupwindow.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.popupwindow.js b/jquery.popupwindow.js index 3be69cd..50c82fc 100644 --- a/jquery.popupwindow.js +++ b/jquery.popupwindow.js @@ -70,7 +70,7 @@ // determine whether to open window // the user wants to always refresh, the handle says its closed, the href is a new page - if (options.forcerefresh || $.popupWindow.win[winPOS].win.closed || winHref === "about:blank") { + if (options.forcerefresh || $.popupWindow.win[winPOS].win.closed || typeof winHref !== "undefined") { $.popupWindow.win[winPOS].win = window.open(url, name, params.join(',')); } From 91bc37efb2e50a8207d17f52182ce4c559f7ede2 Mon Sep 17 00:00:00 2001 From: Todd Horst Date: Sat, 19 Apr 2014 12:39:25 -0400 Subject: [PATCH 5/7] Fix typo --- jquery.popupwindow.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.popupwindow.js b/jquery.popupwindow.js index 50c82fc..41d3c72 100644 --- a/jquery.popupwindow.js +++ b/jquery.popupwindow.js @@ -64,7 +64,7 @@ winPOS = $.popupWindow.win.length - 1; // try regaining access to the handle - $.popupWindow.win[winPOS].win = window.open("", name, paramsJoin); + $.popupWindow.win[winPOS].win = window.open("", name, params.join(',')); try { winHref = $.popupWindow.win[winPOS].win.location.href; } catch (e) { } } From 2111ada9ec43bf99e6244c0a7c5d7fbfb6616b7e Mon Sep 17 00:00:00 2001 From: Todd Horst Date: Sat, 19 Apr 2014 12:55:55 -0400 Subject: [PATCH 6/7] change back, sorry --- jquery.popupwindow.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.popupwindow.js b/jquery.popupwindow.js index 41d3c72..5e4b7d4 100644 --- a/jquery.popupwindow.js +++ b/jquery.popupwindow.js @@ -70,7 +70,7 @@ // determine whether to open window // the user wants to always refresh, the handle says its closed, the href is a new page - if (options.forcerefresh || $.popupWindow.win[winPOS].win.closed || typeof winHref !== "undefined") { + if (options.forcerefresh || $.popupWindow.win[winPOS].win.closed || winHref === "about:blank") { $.popupWindow.win[winPOS].win = window.open(url, name, params.join(',')); } From 110ceaa5ca362cb550e2a7e757e20250b8bafa4a Mon Sep 17 00:00:00 2001 From: Todd Horst Date: Sat, 19 Apr 2014 13:31:57 -0400 Subject: [PATCH 7/7] Better notes --- jquery.popupwindow.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/jquery.popupwindow.js b/jquery.popupwindow.js index 5e4b7d4..8f7fb80 100644 --- a/jquery.popupwindow.js +++ b/jquery.popupwindow.js @@ -50,26 +50,31 @@ var random = new Date().getTime(); var name = options.name || (options.createNew ? 'popup_window_' + random : 'popup_window'); - // get existing win handle if exists - if (!$.popupWindow.win) { $.popupWindow.win = []; } + // 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 global if it didnt already exist + // 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 + // 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(',')); }