Share via

Javascript not running when page is published

Anonymous
2015-11-03T04:11:48+00:00

Hi, I am embedding a javascipt using the script web part on my page.  The code works perfectly when I and 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).  Lots of suggestings around using ExecuteOrDelayUntilScriptLoaded(loadWebs, "sp.js");  or other variants but none work for me.

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

<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>

Microsoft 365 and Office
Microsoft 365 and Office

A comprehensive suite of productivity tools and cloud services that enhance collaboration, communication, and efficiency. Combining classic Office apps with advanced Microsoft 365 features, it supports both personal and business needs

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Anonymous
    2015-11-03T07:51:58+00:00

    Was this answer helpful?

    0 comments No comments