Skip to content

Instantly share code, notes, and snippets.

@jsgao0
Last active July 2, 2016 16:42
Show Gist options
  • Save jsgao0/98c19fcf72354541447d2b6426d86cf7 to your computer and use it in GitHub Desktop.
Save jsgao0/98c19fcf72354541447d2b6426d86cf7 to your computer and use it in GitHub Desktop.
Check is first-page-load using Cookie.
<!DOCTYPE>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<div id="pageLoadStatus"></div>
<script>
var _ = {};
/**
* Gets or sets cookies
* @param name
* @param value (null to delete or undefined to get)
* @param options (domain, expire (in days))
* @return value or true
*/
_.cookie = function(name, value, options)
{
if (typeof value === "undefined") {
var n, v,
cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
n = $.trim(cookies[i].substr(0,cookies[i].indexOf("=")));
v = cookies[i].substr(cookies[i].indexOf("=")+1);
if (n === name){
return unescape(v);
}
}
} else {
options = options || {};
if (!value) {
value = "";
options.expires = -365;
} else {
value = escape(value);
}
if (options.expires) {
var d = new Date();
d.setDate(d.getDate() + options.expires);
value += "; expires=" + d.toUTCString();
}
if (options.domain) {
value += "; domain=" + options.domain;
}
if (options.path) {
value += "; path=" + options.path;
}
document.cookie = name + "=" + value;
}
};
var hasLoadedBefore = _.cookie('hasLoadedBefore');
if(!!hasLoadedBefore) $('#pageLoadStatus').text('Hi! Welcome to here again.'); // When this page has been loaded before.
else $('#pageLoadStatus').text('Hello! It\'s nice to see you here. We have a news for you.'); // When this page loaded at first time.
_.cookie('hasLoadedBefore', true);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment