forked from hpbuniat/jquery-popunder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.popunder.js
executable file
·335 lines (294 loc) · 9.3 KB
/
jquery.popunder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*!
* jquery-popunder
*
* @fileoverview jquery-popunder plugin
*
* @author Hans-Peter Buniat <[email protected]>
* @copyright 2012 Hans-Peter Buniat <[email protected]>
* @license http://opensource.org/licenses/BSD-3-Clause
*
* @requires jQuery
*/
/*global jQuery, window, screen, opener, top */
(function($, window, screen) {
"use strict";
/**
* Create a popunder
*
* @param {Array|function} aPopunder The popunder(s) to open
* @param {string|object} form A form, where the submit is used to open the popunder
* @param {string|object} trigger A button, where the mousedown & click is used to open the popunder
*
* @return jQuery
*/
$.popunder = function(aPopunder, form, trigger) {
var h = $.popunder.helper;
if (trigger || form) {
h.bindEvents(aPopunder, form, trigger);
}
else {
aPopunder = (typeof aPopunder === 'function') ? aPopunder() : aPopunder;
h.c = 0;
h.queue(aPopunder).queue(aPopunder);
}
return $;
};
/* several helper functions */
$.popunder.helper = {
/**
* Reference to the window
*
* @var window
*/
_top: window.self,
/**
* Reference to the last popup-window
*
* @var boolean
*/
lastWin: null,
/**
* Reference to the last url
*
* @var string
*/
lastTarget: null,
/**
* The counter of opened popunder
*
* @var int
*/
c: 0,
/**
* Was the last popunder was processed
*
* @var boolean
*/
last: false,
/**
* About:blank
*
* @var string
*/
b: 'about:blank',
/**
* The last opened window-url (before calling href)
*
* @var string
*/
o: null,
/**
* Chrome?
*
* @var boolean
*/
g: $.browser.webkit,
/**
* Process the queue
*
* @param {Array} aPopunder The popunder(s) to open
*
* @return $.popunder.helper
*/
queue: function(aPopunder) {
var b = false,
h = this;
if (aPopunder.length) {
while (b === false) {
var p = aPopunder.shift();
b = h.open(p[0], p[1] || {}, aPopunder.length);
}
}
else if (h.last === false && (!h.g || h.c === 0)) {
h.last = true;
h.bg().href(true);
}
return this;
},
/**
* Create a popunder
*
* @param {Array} aPopunder The popunder(s) to open
* @param {string|object} form A form, where the submit is used to open the popunder
* @param {string|object} trigger A button, where the mousedown & click is used to open the popunder
*
* @return void
*/
bindEvents: function(aPopunder, form, trigger) {
var a = function() {
$.popunder(aPopunder);
return true;
};
if (form) {
form = (typeof form === 'string') ? $(form) : form;
form.on('submit', $.proxy(a, this));
}
if (trigger) {
trigger = (typeof trigger === 'string') ? $(trigger) : trigger;
trigger.on((this.g === true) ? 'click mousedown' : 'click', $.proxy(a, this));
}
},
/**
* Helper to create a (optionally) random value with prefix
*
* @param {string} sUrl The url to open
* @param {object} options Options for the Popunder
*
* @return boolean
*/
cookieCheck: function(sUrl, options) {
var name = this.rand(options.cookie, false),
cookie = $.cookies.get(name),
ret = false;
if (!cookie) {
cookie = sUrl;
}
else if (cookie.indexOf(sUrl) === -1) {
cookie += sUrl;
}
else {
ret = true;
}
$.cookies.set(name, cookie, {
expiresAt: new Date((new Date()).getTime() + options.blocktime * 3600000)
});
return ret;
},
/**
* Helper to create a (optionally) random value with prefix
*
* @param {string|boolean} name
* @param {boolean} rand
*
* @return string
*/
rand: function(name, rand) {
var p = (name) ? name : 'pu';
return p + (rand === false ? '' : Math.floor(89999999*Math.random()+10000000));
},
/**
* Open the popunder
*
* @param {string} sUrl The URL to open
* @param {object} options Options for the Popunder
* @param {int} iLength Length of the popunder-stack
*
* @return boolean
*/
open: function(sUrl, options, iLength) {
var h = this;
h.o = (h.g) ? h.b : sUrl;
if (top !== window.self) {
try {
if (top.document.location.toString()) {
h._top = top;
}
} catch (err) {}
}
options.disableOpera = options.disableOpera || true;
if (options.disableOpera === true && $.browser.opera === true) {
return false;
}
options.blocktime = options.blocktime || false;
options.cookie = options.cookie || 'puCookie';
if (options.blocktime && (typeof $.cookies === 'object') && h.cookieCheck(sUrl, options)) {
return false;
}
/* create pop-up */
h.c++;
h.lastTarget = sUrl;
h.lastWin = (h._top.window.open(h.o, h.rand(), h.getOptions(options)) || h.lastWin);
if (!h.g) {
h.bg();
}
h.href(iLength);
return true;
},
/**
* Move a popup to the background
*
* @param {int|boolean} l True, if the url should be set
*
* @return $.popunder.helper
*/
bg: function(l) {
var t = this;
if (t.lastWin) {
t.lastWin.blur();
t._top.window.blur();
t._top.window.focus();
if (this.lastTarget && !l) {
if ($.browser.msie === true) {
/* classic popunder, used for ie */
window.focus();
try {
opener.window.focus();
}
catch (err) {}
}
else {
/* popunder for e.g. ff, chrome */
(function(e) {
t.flip(e);
try {
e.opener.window.focus();
}
catch (err) {}
})(t.lastWin);
}
}
}
return this;
},
/**
* Set the popunders url
*
* @param {int|boolean} l True, if the url should be set
*
* @return $.popunder.helper
*/
href: function(l) {
var h = this;
if (l && h.lastTarget && h.lastWin && h.lastTarget !== h.b && h.lastTarget !== h.o) {
h.lastWin.document.location.href = h.lastTarget;
}
return h;
},
/**
* In ff4+, chrome21+ we need to trigger a window.open loose the focus on the popup. Afterwards we can re-focus the parent-window
*
* @param e
*
* @return void
*/
flip: function(e) {
if (typeof e.window.mozPaintCount !== 'undefined' || typeof e.navigator.webkitGetUserMedia === "function") {
try {
e.window.open('about:blank').close();
}
catch (err) {}
}
},
/**
* Get the option-string for the popup
*
* @param {object} options
*
* @return {String}
*/
getOptions: function(options) {
return 'toolbar=' + (options.toolbar || '0') +
',scrollbars=' + (options.scrollbars || '1') +
',location=' + (options.location || '1') +
',statusbar=' + (options.statusbar || '1') +
',menubar=' + (options.menubar || '0') +
',resizable=' + (options.resizable || '1') +
',width=' + (options.width || (screen.availWidth - 122).toString()) +
',height=' + (options.height || (screen.availHeight - 122).toString()) +
',screenX=' + (options.screenX || '0') +
',screenY=' + (options.screenY || '0') +
',left=' + (options.left || '0') +
',top=' + (options.top || '0');
}
};
})(jQuery, window, screen);