Created
March 8, 2017 10:36
-
-
Save evolutionxbox/9de25f71b4fb2ae9e9fbbc27e3de3742 to your computer and use it in GitHub Desktop.
Link Out-of-Hours Redirect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//--------------------------------------------------------------------- | |
function dateObj(d) { | |
var parts = d.split(/:|\s/), | |
date = new Date(); | |
if (parts.pop().toLowerCase() == 'pm') parts[0] = (+parts[0]) + 12; | |
date.setHours(+parts.shift()); | |
date.setMinutes(+parts.shift()); | |
return date; | |
} | |
var endTime = '6:30 AM'; | |
var startTime = '9:45 PM'; | |
var now = new Date(); | |
var endDate = dateObj(endTime); | |
var startDate = dateObj(startTime); | |
// Edit this object, with the anchor id, and the new url | |
var anchors = [ | |
{ | |
selector: '#whatever1', | |
href: 'https://google.com' | |
}, | |
{ | |
selector: '#whatever2', | |
href: 'https://google.com' | |
} | |
]; | |
if (now < endDate || now > startDate) { | |
anchors.forEach(function(anchor) { | |
document.querySelectorAll(anchor.selector).forEach(link => link.href(anchor.href)); | |
}); | |
} | |
//--------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment