-
Notifications
You must be signed in to change notification settings - Fork 3
/
redirect.confirm.js
86 lines (75 loc) · 3.43 KB
/
redirect.confirm.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
// Redirect Confirm
// Version : 1.0.2
// Developer : Ekrem KAYA
// Website : http://e-piksel.com
// GitHub : https://github.com/epiksel/redirect-confirm
!(function ($) {
$.fn.RedirectConfirm = function(options) {
var defaults = {
selector: 'a',
excluding: 'data-rc-exclude', // excluding attribute
title: 'Exiting our website',
message: 'You are now leaving our website. We are not responsible for any external Web sites or their content.',
continuelbl: 'Continue',
returnlbl: 'Return',
targetUrl: '_blank'
};
var options = $.extend(defaults, options);
var confirmed = false;
var getDomain = function(hostname) {
var s = hostname.split(',');
return s.slice(-2).join('.');
};
var currentDomain = getDomain(location.hostname);
var link = new Array();
$(options.selector).each(function() {
var $modal = $('<div id="redirectconfirm-modal" class="modal fade">\
<div class="modal-dialog">\
<div class="modal-content">\
<div class="modal-header">\
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>\
<h3>' + options.title + '</h3>\
</div>\
<div class="modal-body">\
<p>' + options.message + '</p>\
</div>\
<div class="modal-footer">\
<a href="#" class="btn btn-default" data-dismiss="modal">' + options.returnlbl + '</a>\
<a href="' + $(this).attr("href") + '" target="' + options.targetUrl + '" class="btn btn-primary btn-continue">' + options.continuelbl + '</a>\
</div>\
</div>\
</div>\
</div>').appendTo('body');
var $a = jQuery(this);
var $exclude = $a.attr(options.excluding);
if ($exclude == 'true' && $exclude != 'undefined') {
$a.attr(options.excluding, true);
}
if ($a.get(0).hostname && getDomain($a.get(0).hostname) != currentDomain && !$exclude) {
$a.click(function(event) {
if (!confirmed) {
link.push($(this).attr("href"));
event.preventDefault();
event.stopPropagation();
$modal.on('show', function() {
$modal.find('.btn-continue').click(function() {
confirmed = true;
$a.get(0).click();
$modal.modal('hide');
location.reload();
});
});
$modal.on('hide', function() {
confirmed = false;
});
$modal.find('.btn-continue').click(function() {
$modal.modal('hide');
});
$modal.find('.modal-body > p').html(options.message.replace('{url}', $a.attr('href')));
$modal.modal('show');
}
});
}
});
}
})(jQuery);