Skip to content

Instantly share code, notes, and snippets.

@phillipharding
Created January 11, 2016 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save phillipharding/c191c003fd50803dfc83 to your computer and use it in GitHub Desktop.
Save phillipharding/c191c003fd50803dfc83 to your computer and use it in GitHub Desktop.
(function (window, undefined) {
"use strict";
var $ = window.jQuery;
var document = window.document;
// SPHostUrl parameter name
var SPHostUrlKey = "SPHostUrl";
// Gets SPHostUrl from the current URL and appends it as query string to the links which point to current domain in the page.
$(document).ready(function () {
ensureSPHasRedirectedToSharePointRemoved();
var spHostUrl = getSPHostUrlFromQueryString(window.location.search);
var currentAuthority = getAuthorityFromUrl(window.location.href).toUpperCase();
if (spHostUrl && currentAuthority) {
appendSPHostUrlToLinks(spHostUrl, currentAuthority);
}
});
// Appends SPHostUrl as query string to all the links which point to current domain.
function appendSPHostUrlToLinks(spHostUrl, currentAuthority) {
$("a")
.filter(function () {
var authority = getAuthorityFromUrl(this.href);
if (!authority && /^#|:/.test(this.href)) {
// Filters out anchors and urls with other unsupported protocols.
return false;
}
return authority.toUpperCase() == currentAuthority;
})
.each(function () {
if (!getSPHostUrlFromQueryString(this.search)) {
if (this.search.length > 0) {
this.search += "&" + SPHostUrlKey + "=" + spHostUrl;
}
else {
this.search = "?" + SPHostUrlKey + "=" + spHostUrl;
}
}
});
}
// Gets SPHostUrl from the given query string.
function getSPHostUrlFromQueryString(queryString) {
if (queryString) {
if (queryString[0] === "?") {
queryString = queryString.substring(1);
}
var keyValuePairArray = queryString.split("&");
for (var i = 0; i < keyValuePairArray.length; i++) {
var currentKeyValuePair = keyValuePairArray[i].split("=");
if (currentKeyValuePair.length > 1 && currentKeyValuePair[0] == SPHostUrlKey) {
return currentKeyValuePair[1];
}
}
}
return null;
}
// Gets authority from the given url when it is an absolute url with http/https protocol or a protocol relative url.
function getAuthorityFromUrl(url) {
if (url) {
var match = /^(?:https:\/\/|http:\/\/|\/\/)([^\/\?#]+)(?:\/|#|$|\?)/i.exec(url);
if (match) {
return match[1];
}
}
return null;
}
// If SPHasRedirectedToSharePoint exists in the query string, remove it.
// Hence, when user bookmarks the url, SPHasRedirectedToSharePoint will not be included.
// Note that modifying window.location.search will cause an additional request to server.
function ensureSPHasRedirectedToSharePointRemoved() {
var SPHasRedirectedToSharePointParam = "&SPHasRedirectedToSharePoint=1";
var queryString = window.location.search;
if (queryString.indexOf(SPHasRedirectedToSharePointParam) >= 0) {
if (window.history.pushState) {
var safeUrl = window.location.pathname + queryString.replace(SPHasRedirectedToSharePointParam, '');
window.history.pushState(null, document.title, safeUrl);
} else {
window.location.search = queryString.replace(SPHasRedirectedToSharePointParam, "");
}
}
}
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment