-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfirst-visit.js
69 lines (54 loc) · 1.54 KB
/
first-visit.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
/*
* First Visit Js jQuery Plugin version 0.1.2
* Roman Pushkin - roman.pushkin@gmail.com
*/
;(function($){
'use strict';
$.fn.firstVisit = function(options) {
if (!window.localStorage) {
console.warn('localStorage is not defined, add Modernizr referece: https://github.com/Modernizr/Modernizr');
return;
}
var VISIT_KEY_NAME = 'first_visit_key';
var visitKey = false;
var $elements = $(this);
var settings = {
classToAdd: '',
currentPathOnly: false,
currentPathStartsWith: ''
};
$.extend(settings, options);
if(settings.currentPathOnly && settings.currentPathStartsWith)
{
if(window.location.pathname.indexOf(settings.currentPathStartsWith) != 0)
{
console.warn('Incorrect usage of currentPathStartsWith.');
return;
}
}
var getVisitKeyName = function() {
return settings.currentPathOnly ?
VISIT_KEY_NAME + '_' +
(settings.currentPathStartsWith ? settings.currentPathStartsWith : window.location.pathname) + '_'
: VISIT_KEY_NAME;
}
var setVisitKey = function() {
var date = new Date();
window.localStorage.setItem(getVisitKeyName(), date);
}
var getVisitKey = function() {
return window.localStorage.getItem(getVisitKeyName());
}
// get last visit variable
if(!visitKey) {
visitKey = getVisitKey();
}
if(!visitKey) {
setVisitKey();
// show
$elements.show();
// add class if any
if(settings.classToAdd) { $elements.addClass(settings.classToAdd); }
}
}
})(jQuery);