2

using SharePoint 2013 I've added a blog post. This blog post requires me to run some JavaScript after it has been loaded. I am also using jQuery, so I tried the standard $(myInitFunction) to no avail - it fires before the blog post content is loaded.

I also tried SP.SOD.executeOrDelayUntilEventNotified but I'm not sure which event to subscribe to, not could I find a list of event names.

I also tried a few other things that didn't work.

So, how can I execute a JavaScript function after the blog post has finished loading?

5

Since in SharePoint 2013 was introduced Client Side Rendering that represents HTML/JavaScript rendering framework for list views & forms (follow, for example, this article for an introduction), below is demonstrated how invoke a custom code in OnPostRender event:

SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() {

  function init() {
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
      OnPostRender: function(ctx) {
         //your code goes here...
      }
    });
  }

  RegisterModuleInit(SPClientTemplates.Utility.ReplaceUrlTokens("~siteCollection/Style Library/blog.js"), init);
  init();

});

How to apply the changes

  1. Save the specified script as blog.is file and upload it into Style Library
  2. Go to page Edit mode and then open Blogs web part in Edit mode
  3. For JSLink property specify: ~siteCollection/Style Library/blog.js
  4. Save the changes
1

Have you tried this

<script language="javascript">  
    _spBodyOnLoadFunctionNames.push("StartFunction");   
    function StartFunction()   
    {   
       alert('Init');
    }   
</script>

Update

function LoadAfterFunction(){
    //your code
}

ExecuteOrDelayUntilScriptLoaded(LoadAfterFunction, "sp.ui.blogs.js")
  • Yes, and I meant to add that to the list of things I've tried. This appears to happen too soon as well. Apparently the blog content is loaded asynchronously? – Josh M. Apr 10 '15 at 17:32
  • @JoshM. try my updated answer. – Amal Hashim Apr 10 '15 at 17:38
  • Okay, just tried your latest suggestion as well as SP.SOD.executeOrDelayUntilScriptLoaded but my init function is never fired (debugging in Chrome). Even though I can clearly see that "sp.ui.blogs.js" has been loaded... – Josh M. Apr 10 '15 at 17:51
  • For the time being I've resorted to executing my init function on page load and then again 2 seconds later, since it is re-runnable, but I really hate doing that. – Josh M. Apr 10 '15 at 17:53
1

Perhaps this will work for you. It's worth a try

$(window).load(function(){
  //your code
}

This triggers after everything is loaded according to the jQuery docs. However this does not include asynchronous loading according to the docs below.

http://learn.jquery.com/using-jquery-core/document-ready/

Another trick (in SharePoint 2010 at least) is to add the JS code in a webpart below the webpart you are trying to modify using jQuery.

Also possibly related are the settings of the blogpost webpart (asuming there is one), maybe settings there can be changed to prevent async loading.

  • Right, but it has to happen after the blog post is asynchronously loaded. – Josh M. Apr 10 '15 at 20:07
  • I'm no expert on SP2013 but is it not possible to override The view of the blogpost webpart and add the script to the bottom there? – Jan Jouke Tjalsma Apr 10 '15 at 20:09
  • Yes but I need it to happen for every blog post, or close to it. So it's not feasible to do this for each and every one. This JS is included on the page already, I just need to hook up to some event to run it after the blog post is loaded. – Josh M. Apr 10 '15 at 20:11
0

You can test the HTML Placeholder where the blog post will append. While it's empty don't do anything. When the content is there, execute your code. Just use a function with a setTimeout that calls itself and that tests if the content arrived. Make sense?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.