Skip to content

Instantly share code, notes, and snippets.

@thebagg
thebagg / todaysTate.js
Created December 18, 2013 07:00
Get todays date in DD/MM/YYYY format.
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} today = dd+'/'+mm+'/'+yyyy;
document.write(today);
@thebagg
thebagg / verticalCenter.html
Created December 11, 2013 04:52
Vertical Center Anything
.wrapper {
/* set the height of the element which contains what you want to center */
height: 100%;
/* these styles are optional, to set width horizontally center the page */
max-width: 500px;
margin: 0 auto;
}
.wrapper:before, .container {
/* these are the important styles for the centered element: */
display: inline-block;
@thebagg
thebagg / clearFloats
Created September 4, 2013 01:27
Clearing floated elements
/* Assuming this HTML structure:
<div class="clear">
<div class="floated"></div>
<div class="floated"></div>
<div class="floated"></div>
</div>
*/
.clear:before, .clear:after {
@thebagg
thebagg / randomNumber
Created September 2, 2013 03:35
Get a random number in javascript
var url = "http://www.mypage.com/index.php?"+new Date().getTime();
@thebagg
thebagg / equalHeight
Created August 30, 2013 07:59
Equal Height columns.
var highestCol = Math.max($('#element1').height(),$('#element2').height());
$('.elements').height(highestCol);
@thebagg
thebagg / isformdirty
Created August 14, 2013 08:49
Stop a user navigating away from a page. Check
var preventUnloadPrompt;
var messageBeforeUnload = "my message here - Are you sure you want to leave this page?";
$(document).live('click','a', function() { preventUnloadPrompt = true; });
$(document).on('submit','form', function() { preventUnloadPrompt = true; });
$(window).bind("beforeunload", function(e) {
var rval;
if(preventUnloadPrompt) {
return;
} else {
@thebagg
thebagg / stopbrowserloading
Created August 14, 2013 08:45
Replicate pressing ESC key in browser to stop page loading. Check if browser supports window.stop for IE.
function stopBrowser() {
if (window.stop !== undefined) {
window.stop();
}
else if (document.execCommand !== undefined) {
document.execCommand("Stop", false);
}
}
@thebagg
thebagg / convertBytes
Created August 14, 2013 08:44
Convert bytes to Kilobytes, Megabytes, gigabytes or terrabytes.
function bytesToSize(bytes, precision) {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
var posttxt = 0;
if (bytes == 0) return 'n/a';
while( bytes >= 1024 ) {
posttxt++;
bytes = bytes / 1024;
}
return bytes.toFixed(precision) + " " + sizes[posttxt];
}
@thebagg
thebagg / vertalign2.html
Last active December 20, 2015 22:48
Absolute center horizontal and vertical CSS only
<ul>
<li>
<p>Some Text</p>
</li>
<li>
<p>A bit more text that goes on 2 lines</p>
</li>
<li>
<p>Even more text that demonstrates how lines can span multiple lines</p>
</li>
@thebagg
thebagg / trycatch
Created August 5, 2013 04:39
Try/Catch
var result;
result = (function() {
try {
return readFromCache();
} catch (_error) {
return generate();
}
})();