Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
47 changes: 38 additions & 9 deletions jquery.popupwindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
status: false,
toolbar: false,
top: 0,
width: 500
width: 500,
forcerefresh:true
};

$.popupWindow = function(url, opts) {
Expand All @@ -45,27 +46,55 @@
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();
}
}, 50);
}

// 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);