Created
January 15, 2015 22:40
-
-
Save teyc/15d499a265349b4f2c6a to your computer and use it in GitHub Desktop.
Load scripts relative to Sharepoint Site Collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<script type="text/javascript" src="spLoadScripts.js"> | |
Readify.SiteUrl = "/sites/MySite"; | |
Readify.loadScriptsAsync( | |
"SiteAssets/pageStyle.css", | |
"SiteAssets/lodash.js" | |
); | |
</script> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Readify = (function (Readify) { | |
Readify.SiteUrl = null; // unknown | |
Readify.loadScriptsWithSiteUrl = function () { | |
var siteUrl = Readify.SiteUrl; | |
var scriptFiles = arguments; | |
for (var i=0; i < scriptFiles.length; i++) | |
{ | |
var scriptFile = scriptFiles[i]; | |
var scriptUrl = siteUrl + '/' + scriptFile; | |
var extension = scriptFile.substring(scriptFile.length - 4, scriptFile.length); | |
if (extension == ".css") | |
{ | |
if (document.createStyleSheet) { | |
// MSIE 8 | |
document.createStyleSheet(scriptUrl); | |
} else { | |
$('<link>') | |
.appendTo('head') | |
.attr( {type : 'text/css', rel : 'stylesheet'}) | |
.attr('href', scriptUrl); | |
} | |
} else { | |
// don't use getScript because it's async | |
//$.getScript(scriptUrl); | |
$.ajax({ | |
async: false, | |
url: scriptUrl, | |
dataType: 'script' | |
}); | |
} | |
} | |
} | |
Readify.loadScriptsAsync = function() { | |
// loads scripts and CSS relative to site url | |
// sample: loadScripts('app.js', 'app.css', ...); | |
var scriptFiles = Array.prototype.slice(arguments); | |
var ctx = SP.ClientContext.get_current(); | |
var site = ctx.get_site(); | |
ctx.load(site); | |
ctx.executeQueryAsync(function() { | |
var siteUrl = site.get_url(); | |
Readify.SiteUrl = siteUrl; | |
loadScriptsWithSiteUrl(scriptFiles); | |
}); | |
} | |
return Readify; | |
})(Readify || {} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment