Skip to content

Instantly share code, notes, and snippets.

@jdlrobson
Created December 13, 2011 12:15
Show Gist options
  • Save jdlrobson/1471930 to your computer and use it in GitHub Desktop.
Save jdlrobson/1471930 to your computer and use it in GitHub Desktop.
A script using cookies to create a dismissable notification bar at the top of a webpage
// A script to create a dismissable notification bar at the top of a webpage
// uses cookies but would be good to use localStorage where possible.
$(document).ready(function() {
var NOTIFICATION_NAME = "HAS_DISMISSED_NOTIFICATION_3"; // give this a unique name
// this looks at the url and if encounters /en/ prints a english message
// if /es/ prints a spanish message
// on the root / it uses english by default
var isEnglish = window.location.pathname.indexOf("/en/") > -1 || window.location.pathname === "/";
var isSpanish = window.location.pathname.indexOf("/es/") > -1;
var html_en = ["Help! In order to inform our next development phase of ilga.org, we would like to hear your views on how we could make sure that you benefit from the most useful and pleasant experience when accessing ilga.org.<br/>",
"Please <a href='http://www.surveymonkey.com/s/ILGAwebsiteSurvey'>take our survey</a> now!"].join("");
var html_es = ["Link to Spanish questionairre <br/>",
"Please <a href='http://www.surveymonkey.com/s/ILGAencuestaPaginaWeb'>take our survey</a> "].join("");
var html = isEnglish ? html_en : (isSpanish ? html_es : false);
// if not able to detect language exit
if(!html) {
return;
}
function cancelNotification(ev) {
var val = new Date().getTime();
document.cookie = NOTIFICATION_NAME + "=" + val;
$("#notification").slideUp(1000);
}
function showNotification() {
var style = {
padding: "10px",
"background-color": "white",
"border-bottom": "solid 5px green",
"padding-right": "50px"
};
var styleButton = {
//TODO: give close button icon
position: "absolute",
top: "2px",
right: "2px",
"background-image": "url(/ilga/static/close.gif)",
display: "block",
width: "12px",
height: "12px",
overflow: "hidden",
"text-indent": "999px",
"background-color": "black",
"background-repeat": "no-repeat"
};
var notification = $("<div id='notification' style='display:none' />").css(style).html(html).prependTo(document.body).slideDown(1000)[0];
$("<a href='#'>dismiss</a>").click(cancelNotification).css(styleButton).appendTo(notification);
}
var cookie = document.cookie.split(";");
var ok = true;
for(var i = 0; i < cookie.length; i++) {
var nameval = cookie[i].split("=");
if($.trim(nameval[0]) === NOTIFICATION_NAME) {
ok = false;
}
}
if(ok) {
showNotification();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment