0

I cannot determine why javascript function is not being called when paging through search result pages. On the first page load js executes as expected. When I do a hard refresh it executes TruncateComments function as expected. When I page to page 2 only "test" and "undefined" are printed to the console. How do I set this up, so the TruncateComments gets executed on a hard page refresh and paging ahead?

RegisterSod("jquery-3.2.1.min.js", Srch.U.replaceUrlTokens("~sitecollection/_catalogs/masterpage/custom/scripts/jquery-3.2.1.min.js"));
RegisterSod("jquery.truncate.js", Srch.U.replaceUrlTokens("~sitecollection/_catalogs/masterpage/custom/scripts/jquery.truncate.js"));
RegisterSodDep("jquery.truncate.js", "jquery-3.2.1.min.js");

AddPostRenderCallback(ctx, function() {
    console.log("test");
    if(typeof truncate === "undefined") {
        console.log("undefined");
        EnsureScriptFunc("jquery.truncate.js", null, function() {
            console.log("EnsureScriptFunc");
            TruncateComments();
        });
    } else {
        console.log("defined");
        TruncateComments();
    }
});

function TruncateComments() {
    console.log("TruncateComments");
    var comments = $('#LWICommentsValue').html();
    var truncatedComments = jQuery.truncate(comments, {
        length: 10
    });
    $('#LWICommentsValue').replaceWith(truncatedComments);
}
0

Paging is asynchronous hence the load events will not get fired.

Your best option is to hook an event handler the paging object click and then call TruncateComments inside that.

Example

$(document).on('click', '#PageLinkNext', function(){alert('test');});

You can use Browser Dev Tools to find the exact id of the element

  • Thanks. Can you provide an example? – obautista Oct 19 '17 at 13:05

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.