Create a gist now

Instantly share code, notes, and snippets.

@beckettkev /Utils.js
Last active Feb 13, 2017

What would you like to do?
Helper functions for extracting the App Web and Host URL - JavaScript (es6)
function getDocumentQueryStrings() {
const sections = document.URL.split('?');
return sections.length > 1 ? sections[1].split('&') : [];
}
function S4() {
return (((1 + Math.random())*0x10000)|0).toString(16).substring(1);
}
module.exports = {
baseUrl(host) {
let hostname = host;
if (hostname && hostname.match(/[-][\w]+[-]/g)) {
hostname = hostname.match(/[-][\w]+[-]/g)[0];
hostname = hostname.substring(1, hostname.length - 1);
hostname = `${window.location.protocol}//twad${hostname}.sharepoint.com${(window.location.port ? ':' + window.location.port : '')}`;
}
return hostname;
},
baseAssetSiteUrl() {
return `${this.baseUrl(window.location.origin)}/sites/`;
},
getGuid() {
return (S4() + S4() + "-" + S4() + "-4" + S4().substr(0,3) + "-" + S4() + "-" + S4() + S4() + S4()).toLowerCase();
},
getQueryStringParameter(param) {
const el = getDocumentQueryStrings().filter(keyPair => keyPair.split('=')[0] === param);
return el.length > 0 ? window.decodeURIComponent(el[0].split('=')[1]) : null;
},
getSpContaxtUrlParams() {
return {
hostWebUrl: window.decodeURIComponent(this.getQueryStringParameter('SPHostUrl')),
appWebUrl: window.decodeURIComponent(this.getQueryStringParameter('SPAppWebUrl')),
spLanguage: window.decodeURIComponent(this.getQueryStringParameter('SPLanguage'))
};
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment