Can't Edit sharepoint page after adding JS thru CEWP

Jul 21, 2014 at 2:22 PM
I have exact scenario like the link below. I am trying to use auto complete (jquery, spservicec, etc.). It works but edit page does not seem to work.

http://sharepoint.stackexchange.com/questions/11741/cant-edit-page-after-adding-custom-web-part

I get following error with the developer tool.

SCRIPT5007: Unable to get property 'PageState' of undefined or null reference
sp.ribbon.js, line 2 character 79472

But I delete the reference to JS file from CEWP then I dont get the above error and able to edit the page where I dropped the cewp.
Coordinator
Jul 21, 2014 at 7:15 PM
Amber:

Can you post your code?

M.
Jul 22, 2014 at 2:47 PM
Edited Jul 22, 2014 at 8:25 PM
Here is the code.
<script type="text/javascript" src="/_layouts/jquery/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="/_layouts/jquery/jquery-ui-1.10.4/jquery-ui.min.js"></script>
<script type="text/javascript" src="/_layouts/jquery/jquery.SPServices-2014.01.min.js"></script>
<link rel="stylesheet" type="text/css" href="/_layouts/jquery/jquery-ui-themes-1.10.4/themes/smoothness/jquery-ui.css"/>

<div class="ui-widget">
    <label for="EmployeeCode">Employee Code</label>
    <input type="text" size="100" id="EmployeeCode">    
    <input type="button" value="OK" id="btnSearch" name="btnSearch" />  
</div>

<script type="text/javascript">

$(document).ready(function(){
    ExecuteOrDelayUntilScriptLoaded(PostLoad,'SP.js');
});
 
function PostLoad() {
    // Hide Text Filter Control 
    var controlID = "ctl00_m_g_3c02ecf9_d389_42bf_ba2a_801aa56cf4af_SPTextSlicerValueTextControl";  
    document.getElementById("WebPartWPQ3").style.display='none';
    $('#btnSearch').click(moveValue);   
    var externalParties = [];
    window.onload = function() {
    window.setTimeout(readyCall, 1000);
    }
        function readyCall(){
          var externalParties = [];
          $().SPServices({
            operation: "GetListItems",
            listName: "Employees",
            CAMLViewFields: "<ViewFields> <FieldRef Name='EmployeeCode' /> <FieldRef Name='JobTitle' /> </ViewFields>",
            async: false,
            completefunc: function (xData, Status) {
              $(xData.responseXML).SPFilterNode("z:row").each(function() {
                externalParties.push($(this).attr("ows_EmployeeCode") + "  --  " +  $(this).attr("ows_JobTitle"));
              });
            }
          });

            $("#EmployeeCode").autocomplete({
                            source: externalParties,
                            minLength: 3
              });
        }   
 }  
 </script>
 <script type="text/JavaScript">
 function moveValue() {
   var EmployeeCodeField = document.getElementById("EmployeeCode");   
   var otherControl = document.getElementById("ctl00_m_g_3c02ecf9_d389_42bf_ba2a_801aa56cf4af_SPTextSlicerValueTextControl");
   //Making sure both fields are not undefined before accessing their properties.
   if(EmployeeCodeField && otherControl){
        var s = EmployeeCodeField.value;        
        s = s.substring(0, s.indexOf('--'));    
      otherControl.value = s.trim(); 
        __doPostBack('<%= btnSearch.UniqueID %>','')
   }else{
   }
}
</script>
Coordinator
Jul 23, 2014 at 8:41 AM
Edited Jul 24, 2014 at 2:32 AM
Amber:

I'm not sure, but you may have a problem because you have two separate script blocks that need to know about each other. You probably should combine them into one. There also may be some timing issues with your code, as you are using several different methods to introduce lags into the process.

M.
Jul 23, 2014 at 1:58 PM
I combined both JS into one and that seems to work. However I remove the following code then the code does not work at all meaning autopopulate does not happen after type in 3 characters into the text box.
window.onload = function() {
    window.setTimeout(readyCall, 1000);
    }
Coordinator
Jul 24, 2014 at 2:35 AM
I don't think you need that timeout. What are you trying to wait for?

I also don't think that this is necessary:
ExecuteOrDelayUntilScriptLoaded(PostLoad,'SP.js');

Just put all of your script inside the $(document).read(). If it doesn't work, the try stepping through it in a debugger and see where the issue is.

M.