Skip to content

Instantly share code, notes, and snippets.

@cagcak
Last active June 6, 2018 11:54
Show Gist options
  • Save cagcak/03b383f8005929ec78685e28541a23b6 to your computer and use it in GitHub Desktop.
Save cagcak/03b383f8005929ec78685e28541a23b6 to your computer and use it in GitHub Desktop.
Calling with data- attribute partial html files
/* Author: cagcak
* Requirement: jQuery >= 1.x
* Usage : place html partial, call as partialBoneCaller(int level) in main html with
* data-include='./partials/nav.html'
*/
function partialBoneCaller (level) {
'use strict'
var includes = $('[data-include]')
var x = '../'
jQuery.each(includes, function () {
if (detectBrowser() === 'ie' ) {
var file = repeatPollyfill(x, level) + 'views/' + $(this).data('include') + '.html'
} else {
var file = x.repeat(level) + 'views/' + $(this).data('include') + '.html'
}
$(this).load(file)
})
}
function detectBrowser() {
var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
var isFirefox = typeof InstallTrigger !== 'undefined';
var isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || safari.pushNotification);
var isIE = /*@cc_on!@*/false || !!document.documentMode;
var isEdge = !isIE && !!window.StyleMedia;
var isChrome = !!window.chrome && !!window.chrome.webstore;
var isBlink = (isChrome || isOpera) && !!window.CSS;
if (isOpera){return 'opera'} else if (isFirefox){ return 'firefox'} else if (isSafari) { return 'safari' } else if (isIE) { return 'ie'; } else if (isEdge) { return 'edge'; } else if (isChrome) { return 'chrome' } else if (isBlink) { return 'blink' }
}
function repeatPollyfill(deep, times) {
if (times <= 0) { return '' } else if (times === 1) { return deep } else {
return deep + repeatPollyfill(deep, times - 1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment