1
            <a title="_#= $htmlEncode(Srch.Res.sb_GoSearch) =#_" class="ms-srch-sb-searchLink" id="_#= $htmlEncode(searchBoxLinkId) =#_" 
                onclick="CustomFunction();" href="javascript: {}">
                <img src="_#= $urlHtmlEncode(imagesUrl) =#_" class="ms-srch-sbLarge-searchImg" id="searchImg" alt="_#= $htmlEncode(Srch.Res.sb_GoSearch) =#_" />
            </a>

I have this bit of code in a searchbox display template. What my function does is redirect the page on click. However when I run the code above it says that the function is undefined. There is nothing wrong with the code as I have tested it.

<!--#_      function CustomFunction()       {
        // get query string value

        // get value of input search box
        var searchb = 'test';
        // create link
        var searchlink= window.location.protocol + '//' + window.location.hostname+'/Dave2/Pages/FAQSearch.aspx?category='+category+'&k='+searchb;
        // redirect to search page
        window.location = searchlink;
    }
_#-->

The function is placed above the html and it is all in the html display template

2

It could be a scoping issue, that your function is only being defined inside the scope of the display template rendering. Try this:

 <a title="_#= $htmlEncode(Srch.Res.sb_GoSearch) =#_" class="ms-srch-sb-searchLink" id="_#= $htmlEncode(searchBoxLinkId) =#_" 
            onclick="window.CustomFunction();" href="javascript:void(0)">
            <img src="_#= $urlHtmlEncode(imagesUrl) =#_" class="ms-srch-sbLarge-searchImg" id="searchImg" alt="_#= $htmlEncode(Srch.Res.sb_GoSearch) =#_" />
        </a>

<!--#_      window.CustomFunction = function(){
    // get query string value

    // get value of input search box
    var searchb = 'test';
    // create link
    var searchlink= window.location.protocol + '//' + window.location.hostname+'/Dave2/Pages/FAQSearch.aspx?category='+category+'&k='+searchb;
    // redirect to search page
    window.location = searchlink;
}
_#-->
0

You need to define your javascript functions in the below area so that it can be called onClick.

<asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">
<SharePoint:UIVersionedContent UIVersion="4" runat="server"><ContentTemplate>
    <SharePoint:CssRegistration Name="forms.css" runat="server"/>
</ContentTemplate></SharePoint:UIVersionedContent>
    <link rel="stylesheet" href="../../Assets/jquery-ui.css"></link>
<script src="../../Assets/jquery-1.11.1.min.js" type="text/javascript"></script>
<script src="../../Assets/jquery-ui.js" type="text/javascript"></script>
<script type="text/javascript">

$(function() {
    $("#tabs").tabs();
    });
</script>

</asp:Content>
  • He is trying to run it on click of a link onclick="CustomFunction();" href="javascript: {}"> and not on load – Robert Lindgren Sep 9 '14 at 13:13
  • oh sorry let me revise my answer – Joseph D Surgeon Sep 9 '14 at 13:15

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.