105 lines
No EOL
3.3 KiB
JavaScript
105 lines
No EOL
3.3 KiB
JavaScript
/****************************************/
|
|
/* global variables */
|
|
/****************************************/
|
|
//Getting the complete URL and slice it by [0] to the directory URL, return the domain name.
|
|
var urlStr = window.location.toString().slice(0,window.location.toString().lastIndexOf(window.location.pathname)+1);
|
|
//Getting the dir name by gathering the pathname and substract it with the last '/'.
|
|
var dirStr = window.location.pathname.substring(1, window.location.pathname.lastIndexOf('/'));
|
|
|
|
/****************************************/
|
|
/* functions */
|
|
/****************************************/
|
|
|
|
function load(urlHTML, elementHTML, urlJS, elementJS) {
|
|
fetch(urlHTML).then(res => {
|
|
return res.text();
|
|
}).then(htmltext => {
|
|
return elementHTML.innerHTML = htmltext;
|
|
}).then(loadedHTML => {
|
|
loadJS(urlJS, elementJS);
|
|
})
|
|
.catch(
|
|
function (err) {
|
|
console.warn('Could not load '+urlHTML+': Add the correct tag ?', err);
|
|
}
|
|
);
|
|
}
|
|
|
|
function loadHTML(url, element) {
|
|
fetch(url).then(res => {
|
|
return res.text();
|
|
}).then(htmltext => {
|
|
element.innerHTML = htmltext;
|
|
}).catch(
|
|
function (err) {
|
|
console.warn('Could not load '+url+': Add the correct tag ?', err);
|
|
}
|
|
);
|
|
}
|
|
|
|
function loadJS(url, element) {
|
|
var scriptTag = document.createElement('script');
|
|
scriptTag.src = url;
|
|
element.appendChild(scriptTag);
|
|
}
|
|
|
|
function loadCSS(url, element) {
|
|
var linkTag = document.createElement('link');
|
|
linkTag.rel = "stylesheet";
|
|
linkTag.href = url;
|
|
element.appendChild(linkTag);
|
|
}
|
|
|
|
function ready(callback) {
|
|
// in case the document is already rendered
|
|
if (document.readyState != 'loading') callback();
|
|
// modern browsers
|
|
else if (document.addEventListener) document.addEventListener('DOMContentLoaded', callback);
|
|
// IE <= 8
|
|
else document.attachEvent('onreadystatechange', function () {
|
|
if (document.readyState == 'complete') callback();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Parse each character of the given string.
|
|
* @param {string} string
|
|
* @returns number of '/' in the string
|
|
*/
|
|
function getSlashNum(string) {
|
|
var returnValue = 0;
|
|
for (char of string) {
|
|
if (char == "/") {
|
|
returnValue++;
|
|
}
|
|
}
|
|
return returnValue;
|
|
}
|
|
|
|
/**
|
|
* Will contenate a string of slash (num of parent dir) onto a given path
|
|
* @param {Int, number of slashes} slashNum
|
|
* @param {Concat String} basePath
|
|
* @returns the concatened string
|
|
*/
|
|
function concatPath(slashNum, basePath)
|
|
{
|
|
var slashString = "";
|
|
for (let i = 0; i < slashNum; i++) {
|
|
slashString += "../";
|
|
}
|
|
return slashString+basePath;
|
|
}
|
|
|
|
/****************************************/
|
|
/* main */
|
|
/****************************************/
|
|
|
|
var headTag = document.getElementsByTagName("head")[0];
|
|
window.addEventListener('load', function () {
|
|
loadCSS(urlStr+"css/base.css", headTag);
|
|
loadCSS(urlStr+"css/navbar.css", headTag);
|
|
load(urlStr+"components/navbar.html", document.getElementsByTagName("Navbar")[0], urlStr+"js/navbar.js", headTag);
|
|
loadCSS(urlStr+"css/footer.css", headTag);
|
|
load(urlStr+"components/footer.html", document.getElementsByTagName("Footer")[0], urlStr+"js/footer.js", headTag);
|
|
}) |