Hi, I am embedding a javascipt using the script web part on a SharePoint publishing page. I am using SharePoint online.
The code works perfectly when I am in edit mode, in fact every stage until the page is published. I have read this is due to the SP.js not being loaded on publishing pages (lots of people seem to be struggling with this).
I hoped using ExecuteOrDelayUntilScriptLoaded(loadWebs, "sp.js"); or other variants would work but I still have the issue.
The code is included below (it lists subsites of the current site). To repro please paste into a script part (it will work) and then fully publish the page (it will break).
<script type="text/javascript">
function loadWebs() {
var clientContext = new SP.ClientContext.get_current();
this.webs = clientContext.get_web().get_webs();
clientContext.load(this.webs);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onWebsLoaded), Function.createDelegate(this, this.onQueryFailed));
} function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace()); }
function onWebsLoaded(sender, args) {
for (var i = 0; i < this.webs.get_count(); i++) {
insRow();
}
function insRow()
{
var x=document.getElementById('myTable').insertRow(0);
var y=x.insertCell(0);
var wts = (this.webs.itemAt(i).get_title());
var toUrl = (this.webs.itemAt(i).get_url());
y.innerHTML='<a href="'+toUrl+'">'+(this.webs.itemAt(i).get_title())+'</a>';
}
}
ExecuteOrDelayUntilScriptLoaded(loadWebs, "sp.js");</script><table id="myTable" border="0"><tr><td></td></tr></table>Any help appreciated.
Thanks
Brendan