Question

I have a Content Editor Web Part in SharePoint 2007 that includes a function to insert several tags on the page with different JavaScript sources.

For some reason, it takes about 4-5 minutes and several refreshes in order to get one of my JavaScript sources (an API) to load. Typically, this will only happen in the morning, upon my first visit to my SharePoint site.

This problem used to happen more frequently, but I have since placed a series of buttons that trigger reloads of the function that includes script source code on the page (see code-block below: Credit to Herve Tourpe)

Could someone give me insight as to why this is happening?

For more information, I am using the MIT Simile Timeline API. Usually, once I log on to the site in the morning the error on the page is that "Timeline" is undefined (which is, clearly, the most integral class from the API file).

To me, this either means that the function that executes the timeline hasn't waited long enough for the API to load, or there is some sort of a problem with the following function:

function includeJSScript(p_file) {
  // before we insert this script, we need to check if it already exists
  var bAlreadyExists = false;
  var scripts = document.getElementsByTagName('script');

  for (var i = 0; i < scripts.length; i++) {
    if (scripts[i].src == p_file) {
    bAlreadyExists = true;
    break;
    }
  }

  if (!bAlreadyExists) {
    var v_script = document.createElement('script');
    v_script.type = 'text/javascript';
    v_script.src = p_file;
    document.getElementsByTagName('head')[0].appendChild(v_script);
  }
}
Was it helpful?

Solution

I think your issue is timing and in general, the Content Editor Web Part.

The problem is that the part gets rendered last and with the overloaded DOM in SharePoint, that means it sometimes won't load fast enough (I've had this problem a lot in Chrome using jQuery). Experiencing it in the morning more often is due to SharePoint's cache.

There are a few ways to fix it and you can work through them each until solved:

To start with: for the slowness, be sure to use SPWakeUp3 on Codeplex - this will 'wake up' (i.e. cache) your sites and scripts; SP dumps the cache every 24 hours - this will fix that.

For the script itself:

Try this: take the code out of the part, put into a text file and upload to a document library - point the CEWP to that file instead.

If no change, try using the HTML/JS Render Web Part from CodePlex - this avoids the general problems with the CEWP: http://davidmsterling.blogspot.com/2012/12/htmljavascript-rendering-web-part.html

If still not working you can try to -

Embed the script into the Page Layout and use the CEWP or HTML/JS Render (again using a File to hold the script call) OR

Embed the script into the Master Page (but this is overhead you probably don't need)

I'm pretty sure one of the above should resolve the issue.

David M. Sterling

http://davidmsterling.blogspot.com

http://www.sterling-consulting.com

http://www.sharepoint-blog.com

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top