-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblinkingtab.js
More file actions
153 lines (132 loc) · 7.69 KB
/
blinkingtab.js
File metadata and controls
153 lines (132 loc) · 7.69 KB
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
/**
* Created by Mohamed Ahmed Fouad on 10/14/14.
*/
(function () {
if (typeof BlinkingTab === 'undefined') {
BlinkingTab = function (options) {
// Define option defaults
var defaults = {
autoStart: true, // set it to false if you don't want to autoStart the plugin (for example if you wish to activate the functionality later by calling BlinkingTab.start())
titleBlink_1: 'NEW TITLE -- NEW TITLE', // the first title will that show when blinking
titleBlink_2: '****', // the second title that will show when blinking
titleWhenTabActive: document.title, // [require revertToPreviousTitle: true] the title that will set when the user come back to your website again
revertToPreviousTitle: false, // set it to true if you wish to revert to another title when the user come back to your website again
delayBeforeBlinkingSeconds: 2, // [require blinkTitle: true] How much time (in seconds) before the title start blinking
blinkIntervalSeconds: 0.5, // [require blinkTitle: true] Interval of blinking between the first title (titleBlink_1) & the second (titleBlink_2)
blinkTitle: true, // True: will blink the title | False: will simply replace the title with titleBlink_1 when the user is inactive
redirectWhenActive: false, // set it to true if you wish to redirect your visitors to another URL when they return to your website
redirectURL: 'http://www.google.com', // [require redirectWhenActive: true] the redirection URL
animateTitle: true // coming soon
};
// Create global variables
BlinkingTab.tabVisible = true; // variable to keep track of the visibility of the tab
BlinkingTab.timer = null; // the timer for the delayBeforeBlinkingSeconds
BlinkingTab.blinkInterval = null; // the timer for the blinker
BlinkingTab.blinkOn = true; // variable to keep track of the status of the blinker
BlinkingTab.titleChanged = false; // variable to check the status of the title whether is already been changed or not
// Create options by extending defaults with the passed in arguments
BlinkingTab.settings = BlinkingTab.extendDefaults(defaults, options);
// Set the name of the hidden property and the change event for visibility
var hidden, visibilityChange;
if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support
hidden = "hidden";
visibilityChange = "visibilitychange";
} else if (typeof document.mozHidden !== "undefined") {
hidden = "mozHidden";
visibilityChange = "mozvisibilitychange";
} else if (typeof document.msHidden !== "undefined") {
hidden = "msHidden";
visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
hidden = "webkitHidden";
visibilityChange = "webkitvisibilitychange";
}
BlinkingTab.settings.visibilityChange = visibilityChange;
BlinkingTab.settings.hidden = hidden;
// start automatically when instantiated
if (BlinkingTab.settings.autoStart === true) BlinkingTab.start();
};
}
if (typeof BlinkingTab.start === 'undefined') {
BlinkingTab.start = function () {
// Warn if the browser doesn't support addEventListener or the Page Visibility API
if (typeof document.addEventListener === "undefined" ||
typeof document[BlinkingTab.settings.hidden] === "undefined") {
console.log("This demo requires a browser, such as Google Chrome or Firefox, that supports the Page Visibility API.");
} else {
// Initialize our event listeners
BlinkingTab.initializeEvents.call();
}
}
}
// Initialize our event listeners
if (typeof BlinkingTab.initializeEvents === 'undefined') {
BlinkingTab.initializeEvents = function () {
// Event to detect visibility change
document.addEventListener(BlinkingTab.settings.visibilityChange, BlinkingTab.handleVisibilityChange, false);
};
}
// Handle page visibility change
if (typeof BlinkingTab.handleVisibilityChange === 'undefined') {
BlinkingTab.handleVisibilityChange = function () {
// Check the current situation of the tab visibility
if (document[BlinkingTab.settings.hidden]) {
// the Tab is inactive (not visible)
BlinkingTab.tabVisible = false;
// Set a timer with delayBeforeBlinkingSeconds, so when it's over we blink the title
BlinkingTab.timer = setTimeout(function () {
// When the time is over we check if the tab is still inactive
if (!BlinkingTab.tabVisible) {
// check if we want to blink the title or not
if (BlinkingTab.settings.blinkTitle)
// set timer with blinkIntervalSeconds to call blinkTitleFunction()
BlinkingTab.blinkInterval = setInterval("BlinkingTab.blinkTitleFunction()", 1000 * BlinkingTab.blinkIntervalSeconds);
else
document.title = BlinkingTab.settings.titleBlink_1; // When we don't want to blink the title, we put only the first blink title
BlinkingTab.titleChanged = true; // set this variable so we can test it later when check for redirection
}
}, 1000 * BlinkingTab.settings.delayBeforeBlinkingSeconds);
} else {
// the Tab is active (visible)
BlinkingTab.tabVisible = true;
// clear the timer to save memory
if (BlinkingTab.timer) clearTimeout(BlinkingTab.timer);
if (BlinkingTab.blinkInterval) clearInterval(BlinkingTab.blinkInterval);
// check if we want to revert to previous title or not
if (BlinkingTab.settings.revertToPreviousTitle)
document.title = BlinkingTab.settings.titleWhenTabActive;
else
document.title = BlinkingTab.settings.titleBlink_1;
// check if we ever changed the title so we can proceed with the redirection process
if (BlinkingTab.titleChanged) {
if (BlinkingTab.settings.redirectWhenActive) // check if we want to redirect the visitor
window.location = BlinkingTab.settings.redirectURL; // redirect the visitor
}
}
}
}
if (typeof BlinkingTab.blinkTitleFunction === 'undefined') {
BlinkingTab.blinkTitleFunction = function () {
if (BlinkingTab.blinkOn) {
document.title = BlinkingTab.settings.titleBlink_1;
BlinkingTab.blinkOn = false;
}
else {
document.title = BlinkingTab.settings.titleBlink_2;
BlinkingTab.blinkOn = true;
}
}
}
// Utility method to extend defaults with user options
if (typeof BlinkingTab.extendDefaults === 'undefined') {
BlinkingTab.extendDefaults = function (source, properties) {
var property;
for (property in properties) {
if (properties.hasOwnProperty(property)) {
source[property] = properties[property];
}
}
return source;
}
}
})();