Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@beckettkev
Last active February 13, 2017 11:28
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 beckettkev/77b7c7cf220826d0ca0f417d8b85e545 to your computer and use it in GitHub Desktop.
Save beckettkev/77b7c7cf220826d0ca0f417d8b85e545 to your computer and use it in GitHub Desktop.
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