Last active
August 29, 2015 14:21
-
-
Save ovdojoey/bf9dc24564bcb79e865c to your computer and use it in GitHub Desktop.
LinkJax - Hijacks links in browser and allows for ajax loaded content
This file contains hidden or 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
/** | |
* linkjax.js v1.0.0 | |
* http://www.joeylea.com | |
* | |
* hijack links on page and turns them into AJAX loaded content | |
* | |
* Licensed under the MIT license. | |
* http://www.opensource.org/licenses/mit-license.php | |
* | |
*/ | |
var LINKJAX = LINKJAX || (function () { | |
'use strict'; | |
// default LINKJAX arguments | |
var _args = { | |
urlPartial: '/partial', | |
returnType: 'html', // html or json | |
linkSelector: '.linkjax', // element selector or false | |
}, | |
LJSupport = true, | |
LJActive = false, | |
loadingHttpRequest = false, | |
host, | |
httpRequest; | |
// new function object | |
function LINK(el) { | |
this.el = el; | |
this.el.addEventListener('click', this, false); | |
} | |
function loadPage() { | |
LJActive = loadingHttpRequest = false; | |
console.log(this); | |
} | |
function pageError() { | |
LJActive = loadingHttpRequest = false; | |
console.log(this); | |
} | |
function getPage(src) { | |
console.log(src); | |
if (loadingHttpRequest) { | |
return; | |
} | |
loadingHttpRequest = true; | |
httpRequest = new XMLHttpRequest(); | |
httpRequest.onload = loadPage; | |
httpRequest.onerror = pageError; | |
// if (src) { oPageInfo.url = filterURL(sPage, null); } | |
httpRequest.open("get", src, true); | |
// if header needs customization - set http request .setRequestHeader('X-LINKJAX-REFERRER', _args.customHeader); | |
httpRequest.send(); | |
// loading | |
} | |
function getPageLinks() { | |
var _links; | |
if (_args.linkSelector) { | |
_links = Array.prototype.slice.call(document.querySelectorAll(_args.linkSelector)); | |
} else { | |
_links = Array.prototype.slice.call(document.links); | |
} | |
return _links; | |
} | |
function init() { | |
// if history API is not supported do not add event listeners and halt execution | |
if (!history.pushState || document.location.origin === 'file://') { | |
LJSupport = false; | |
return false; | |
} | |
// Get current host | |
var a = document.createElement('a'); | |
a.href = '/'; | |
host = a.hostname; | |
var _links = getPageLinks(); | |
// for each same origin link create a click handler | |
_links.forEach(function (el) { | |
if (el.hostname !== host) { | |
return; | |
} | |
new LINK(el); | |
}); | |
} | |
LINK.prototype.handleEvent = function (e) { | |
// No LinkJax support or link was requested in new tab | |
if (!LJSupport || e.metaKey || e.button > 1 || e.which > 1 || e.ctrlKey) { | |
return false; | |
} | |
if (!LJActive) { | |
LJActive = true; | |
e.preventDefault(); | |
this.requestResource(); | |
} | |
}; | |
LINK.prototype.requestResource = function () { | |
var _src = this.el.getAttribute('href'); | |
history.pushState({ href: _src }, '', _src); | |
getPage(_src); | |
}; | |
// Return the init function and allow for LINKJAX args to be passed and overwritten | |
return { | |
init: function (Args) { | |
var _prop; | |
for (_prop in Args) { | |
if (Args.hasOwnProperty(_prop) && _args.hasOwnProperty(_prop)) { | |
_args[_prop] = Args[_prop]; | |
} | |
} | |
init(); | |
} | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment