Skip to content

Instantly share code, notes, and snippets.

@zplume
Last active August 29, 2015 14:02
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 zplume/bc950b1ccd4e5d6cc725 to your computer and use it in GitHub Desktop.
Save zplume/bc950b1ccd4e5d6cc725 to your computer and use it in GitHub Desktop.
Global (tenant scoped) HTML/JS 'web part' framework for SharePoint Online
// replacement function for console.log(), which avoids 'undefined' exception in IE 8
window.LogMsg = function (msg) {
if (window.console) {
console.log(msg);
}
};
// set up root namespaces
window.LS = window.LS || {};
LS.AJAXFrames = LS.AJAXFrames || {};
LS.AJAXFrames.Init = function () {
// don't load the content when the page is in edit mode
if (document.forms[MSOWebPartPageFormName].MSOLayout_InDesignMode.value == 0) {
// check for newly loaded ajax-iframe divs twice per second (as these can be loaded/added to the page after this JS file is loaded)
setInterval(function () {
jQuery('.ajax-iframe').each(function () {
var element = jQuery(this);
var frameUrl = encodeURI(element.attr('data-src'));
LogMsg("Loading ajax frame " + frameUrl);
// show loading message
element.html("<span style='border: 1px solid #eee; margin-top: 10px; padding: 10px; display:inline-block;'><img src='_layouts/images/loading.gif' style='vertical-align:bottom; width:16px; height:16px;' /> Loading...</span>");
jQuery.ajax({
url: frameUrl,
cache: false,
async: true,
dataType: "html"
}).done(function (data) {
setTimeout(function () {
element.html(data);
}, 1000); // show the loading message for at least a second before adding the HTML to the <div>, as this looks nicer
}).fail(function(request, status, err) { LogMsg([request, status, err]); });
element.removeClass('ajax-iframe'); // load the content once, then remove the class (to prevent loading multiple times)
});
}, 500);
}
};
_spBodyOnLoadFunctionNames.push("LS.AJAXFrames.Init"); // don't execute the function until SharePoint is ready
<WebPartPages:ScriptEditorWebPart
runat="server"
Content="&lt;div class=&quot;ajax-iframe&quot; data-src=&quot;/sites/assets/Style Library/HTML/SiteList.html&quot;&gt;&lt;/div&gt;"
ChromeType="TitleOnly"
Description="Displays a list of all site collections in the tenant that the current user has access to."
Title="Site List"
ID="g_89b95591_10e3_4ff9_a4a3_5a5b0a6c9e7f"
WebPart="true"
partorder="2">
</WebPartPages:ScriptEditorWebPart>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment