Skip to content

Instantly share code, notes, and snippets.

@madr
Created February 28, 2012 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madr/1931992 to your computer and use it in GitHub Desktop.
Save madr/1931992 to your computer and use it in GitHub Desktop.
Misc JavaScript Helpers
function adate(daysFromNow) {
var date, datestr, mm, dd;
datestr = new Date().getTime();
if (!!daysFromNow) {
datestr += (1000 * 60 * 60 * 24 * daysFromNow);
}
date = new Date(datestr);
mm = date.getMonth() + '';
if (mm.length == 1) {
mm = '0' + mm;
}
dd = date.getDate() + '';
if (dd.length == 1) {
dd = '0' + dd;
}
return date.getFullYear() + '-' + mm + '-' + dd;
}
// found at http://james.padolsey.com/javascript/get-document-height-cross-browser/
function getDocHeight() {
var D = document;
return Math.max(
Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
Math.max(D.body.clientHeight, D.documentElement.clientHeight)
);
}
// found at http://stackoverflow.com/questions/871399/cross-browser-method-for-detecting-the-scrolltop-of-the-browser-window
function getScrollTop(){
if(typeof pageYOffset!= 'undefined'){
//most browsers
return pageYOffset;
}
else{
var B= document.body; //IE 'quirks'
var D= document.documentElement; //IE with doctype
D= (D.clientHeight)? D: B;
return D.scrollTop;
}
}
function hex2rgb(hex, opacity) {
var rgb = hex.replace('#', '').match(/(.{2})/g);
var i = 3;
while (i--) {
rgb[i] = parseInt(rgb[i], 16);
}
if (typeof opacity == 'undefined') {
return 'rgb(' + rgb.join(', ') + ')';
}
return 'rgba(' + rgb.join(', ') + ', ' + opacity + ')';
};
function getIFrameDocumentElement(iframe) {
return (iframe.contentWindow) ? iframe.contentWindow : (iframe.contentDocument.document) ? iframe.contentDocument.document : iframe.contentDocument;
}
// unset cookie
function(name){
return function(expires) {
return document.cookie = escape(name) + '=' + expires + '; path=/';
}((function(d){
d.setTime(d.getTime() + (86400000 * -1));
return '; expires=' + d.toGMTString();
}(new Date())));
}('name-of-cookie');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment