Skip to content

Instantly share code, notes, and snippets.

@eeeschwartz
Last active May 23, 2016 19:57
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 eeeschwartz/a092332448c45a8c600349319fe73bd8 to your computer and use it in GitHub Desktop.
Save eeeschwartz/a092332448c45a8c600349319fe73bd8 to your computer and use it in GitHub Desktop.
Javascript-based redirects from lexingtonky.gov to next.lexingtonky.gov
node_modules/*

Linting

npm install jshint
node_modules/jshint/bin/jshint redirector.js
(function () {
"use strict";
var lib = {
param: function (nameParam) {
var url = window.location.href,
name = nameParam.replace(/[\[\]]/g, "\\$&"),
regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
},
width: function() {
// vanilla JS window width and height
var w=window,
d=document,
e=d.documentElement,
g=d.getElementsByTagName('body')[0],
x=w.innerWidth||e.clientWidth||g.clientWidth;
return x;
}
};
var redirector = {
maxWidth: 600,
shouldRedirect: function (screenWidth, referrer) {
if (screenWidth > this.maxWidth) { return false; }
// dont redirect internal links
if (referrer.indexOf('lexingtonky.gov') > -1) { return false; }
return true;
},
targetPath: function (pageId) {
return redirectTable[pageId];
},
redirectTo: function (targetPath) {
window.location = "https://next.lexingtonky.gov" + targetPath;
},
redirect: function (pageId, pageWidth, referrer) {
var targetPath = this.targetPath(pageId);
if (this.shouldRedirect(pageWidth, referrer)) { this.redirectTo(targetPath); }
}
};
var redirectTable = {
200: "/browse/recreation/aquatics",
};
redirector.redirect(lib.param("page"), lib.width(), document.referrer);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment