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

Amal Hashim
  • 28,306
  • 5
  • 31
  • 61
Dillydallydave
  • 205
  • 1
  • 4
  • 10

2 Answers2

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

Amal Hashim
  • 28,306
  • 5
  • 31
  • 61
-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.

Avijit Sur
  • 514
  • 2
  • 5