none
Javascript function only works in edit mode

    Question

  • After an upgrade to 2020 my client found that the advanced search web part doesn't accept Enter as the default button.  It must be explicitly clicked to search.  Click Enter takes a user to the home page.  Anyway, here's my problem:

    As a work around I'm subscribing to the page's keydown event and just calling the button's click method.  The problem is, my javascript, added using a Content Query Web Part, only works in Edit Mode.  Any thoughts on why?

    Thank You

    <script
    type="text/javascript">
    
      // call the function that registers the event
    _spBodyOnLoadFunctionNames.push("initializeAll");
    
    function initializeAll()
    {
       // register the keydown event
       document.onkeydown=simulateClick;
    }
    function simulateClick(event)
    {
        
    // only for the enter key
       if (event.keyCode == 13)   
       {
     document.getElementById("ctl00_m_g_ea6e689d_a19d_42b1_8041_559641276719_ASB_BS_SRCH_1").click();   
       }
    }
    </script>



    Nate

    Thursday, April 25, 2013 10:54 PM

Answers

  • I found the underlying issue.  The reason is works in edit mode is because the problem is in my header area, which isn't rendered in edit mode.

    I had an asp.net image button that was preventing other buttons on the page from becoming the default.  Once I realized the issue, I replaced it with the SPLinkButton sharepoint uses on its own master pages and the Search button was freed to become the default button for the page.


    Nate

    Friday, April 26, 2013 1:14 PM

All replies

  • I found the underlying issue.  The reason is works in edit mode is because the problem is in my header area, which isn't rendered in edit mode.

    I had an asp.net image button that was preventing other buttons on the page from becoming the default.  Once I realized the issue, I replaced it with the SPLinkButton sharepoint uses on its own master pages and the Search button was freed to become the default button for the page.


    Nate

    Friday, April 26, 2013 1:14 PM
  • This issue usually occurs in case of the following:

    • The Script tag doesn’t have type=”text/javascript”.
    • The “SP.js” and “SP.Runtime” files are not refrenced properly.
    • You didn’t call the ExecuteOrDelayUntilScriptLoaded function.
    • The Minimal Download Strategy Feature is activated.
    • The code is not written correctly!

    For more details, check SharePoint 2016: JSOM is only working in Edit Mode

    Friday, April 12, 2019 11:45 PM