AJAX Content Loader
// #####################################################################################################################
// # This is a barebones AJAX system I created. Every link in the class "ajax-link" (i.e. <a class="ajax-link">) will #
// # instead perform an XMLHTTPRequest and the response will be placed into an element with the class ".content" #
// # (i.e. <section class="content">). This will also perform the necessary browser history manipulations so the #
// # back/forward buttons still work as expected. Uses jQuery. #
// #####################################################################################################################
// Stores the current state.
var state = window.location.pathname + window.location.hash;
// Changes the URL when an AJAX link is clicked.
$(document).on('click', '.ajax-link', function(event){
event.preventDefault();
// Change the URL and create history (push a new state onto the stack).
if(window.location.pathname !== this.pathname){
history.pushState(null, null, this.href + this.hash);
setState(this.href);
}
});
// Changes the URL when the back button is clicked (a state has been popped off of the stack).
window.onpopstate = function(){
if(window.location.pathname !== state.split('#')[0]){
setState(window.location.href + window.location.hash);
}
};
// Synchronizes content with a state.
function setState(link){
if(typeof setState.content === 'undefined') setState.content = $('.content');
setState.content.load(link, function(){
state = window.location.pathname + window.location.hash;
});
}
Calendar
// Returns a string of the current system date in a verbose format. (i.e. January 1st, 2000)
$(document).ready(function(){$('.calendar').text(calendar())});
function calendar(){
calendar.date = new Date();
calendar.year = calendar.date.getFullYear();
calendar.month = calendar.date.getMonth();
calendar.day = calendar.date.getDate();
// Verbose month.
switch(calendar.month){
case 0: calendar.month = 'January'; break;
case 1: calendar.month = 'February'; break;
case 2: calendar.month = 'March'; break;
case 3: calendar.month = 'April'; break;
case 4: calendar.month = 'May'; break;
case 5: calendar.month = 'June'; break;
case 6: calendar.month = 'July'; break;
case 7: calendar.month = 'August'; break;
case 8: calendar.month = 'September'; break;
case 9: calendar.month = 'October'; break;
case 10: calendar.month = 'November'; break;
case 11: calendar.month = 'December'; break;
}
// Date suffix.
switch(calendar.day){
case 1: calendar.day += 'st'; break;
case 2: calendar.day += 'nd'; break;
case 3: calendar.day += 'rd'; break;
case 21: calendar.day += 'st'; break;
case 22: calendar.day += 'nd'; break;
case 23: calendar.day += 'rd'; break;
case 31: calendar.day += 'st'; break;
default: calendar.day += 'th'; break;
}
return calendar.month + ' ' + calendar.day + ', ' + calendar.year;
}
Clock
// Returns a string of the current system time in the format H:MM:SS AM/PM.
setInterval(function(){$('.clock').text(clock())}, 1000);
function clock(){
clock.time = new Date();
clock.hours = clock.time.getHours();
clock.minutes = clock.time.getMinutes();
clock.seconds = clock.time.getSeconds();
// Leading zeroes.
if(clock.minutes < 10) clock.minutes = '0' + clock.minutes;
if(clock.seconds < 10) clock.seconds = '0' + clock.seconds;
// Calculate ante meridiem or post meridiem.
if(clock.hours > 12){
clock.meridiem = 'PM';
clock.hours -= 12;
}else{
clock.meridiem = 'AM';
}
// Zero hour is midnight.
if(clock.hours == 0) clock.hours = 12;
return clock.hours + ':' + clock.minutes + ':' + clock.seconds + ' ' + clock.meridiem;
}
Disable Invalid Links
$(document).ready(checkLinks);
function checkLinks(){
checkLinks.link = $('a');
for(var i = 0; i < checkLinks.link.length; i++){
$.ajax({
url: checkLinks.link[i].href, type: 'HEAD', error: function(){
$(checkLinks.link[i]).attr('style', 'color: grey !important; cursor: default; pointer-events: none;');
}
});
}
}