Skip to content

Instantly share code, notes, and snippets.

@getsetbro
Forked from phillipharding/aspnetmvc-spcontext.js
Last active October 16, 2017 15:42
Show Gist options
  • Save getsetbro/1b5e30432429cfe6d89937d789b60db9 to your computer and use it in GitHub Desktop.
Save getsetbro/1b5e30432429cfe6d89937d789b60db9 to your computer and use it in GitHub Desktop.
spcontext.js without jquery
/*!spcontext*/
(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().each();
var elements = Array.prototype.filter.call(document.querySelectorAll('a'), function (el,i) {
var authority = getAuthorityFromUrl(el.href);
if (!authority && /^#|:/.test(el.href)) {
// Filters out anchors and urls with other unsupported protocols.
return false;
}
return authority.toUpperCase() == currentAuthority;
});
Array.prototype.forEach.call(elements, function (el, i) {
if (!getSPHostUrlFromQueryString(el.search)) {
if (el.search.length > 0) {
el.search += "&" + SPHostUrlKey + "=" + spHostUrl;
}
else {
el.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) {
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