5

How can I run some JavaScript after my display template has loaded. I have tried:

AddPostRenderCallback(ctx, function () {     
    jQuery(document).ready(function () {
        alert(jQuery('.ms-srch-hover-subTitle').text());      
    });
})

but that doesn't work. .ms-srch-hover-subTitle is getting added from the display template. so I want the display template to be put on screen and then for the js to run

2
  • Can you please explain what you mean by that didn't worked? Commented Dec 23, 2014 at 14:25
  • ran before display template was loaded Commented Dec 23, 2014 at 14:26

2 Answers 2

4

You are registering on load event. Instead modify as follows

AddPostRenderCallback(ctx, function () {     
    alert(jQuery('.ms-srch-hover-subTitle').text());      
})

Also take a look at this Run any other javascript after the Display Templates have rendered the content

5
  • that has the same result Commented Dec 23, 2014 at 14:29
  • can you please explain what .ms-srch-hover-subTitle this control does. Is it getting populated from the display template? Commented Dec 23, 2014 at 14:33
  • yes it's getting populated from the display template Commented Dec 23, 2014 at 14:35
  • Can you please add more code. Commented Dec 23, 2014 at 14:35
  • @Dillydallydave I have added a link. Also check that and make sure you are following the correct way. Commented Dec 23, 2014 at 14:46
-1

SharePoint does provide a few options.

1) Script on Demand: (load a js file then execute my code.)

function stuffThatRequiresSP_JS(){ //your code } SP.SOD.executeFunc("sp.js")

2) Delay until loaded (wait for a js file, then run)

function stuffToRunAfterSP_JS(){ //your code } ExecuteOrDelayUntilScriptLoaded(stuffToRunAfterSP_JS, "sp.js")

3) load after other stuff finishes

function runAfterEverythingElse(){ // your code } _spBodyOnLoadFunctionNames.push("runAfterEverythingElse");

Sources:

executeFunc: http://msdn.microsoft.com/en-us/library/ff409592(v=office.14).aspx

ExecuteOrDelayUntilScriptLoaded: http://msdn.microsoft.com/en-us/library/ff411788(v=office.14).aspx

I found the answer here.

1
  • Please read question carefully. Its not about page load. Commented Dec 23, 2014 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.