JavaScript content count is unreliable with attachEvent for keypress/onclick

Last post 03-12-2006, 3:02 PM by Adam. 1 replies.
Sort Posts: Previous Next
  •  03-09-2006, 1:31 AM 16878

    JavaScript content count is unreliable with attachEvent for keypress/onclick

    I have a user control that contains 3 editors for which I need to update a count of the chars (including the tags), but the events only sometimes bubble.  On load 7/8's of the time the characters remaining will be 0 even though there is text in the CE control.  Clicking or typing in the CE does not cause updates.  Refresh the page a few times and the count will show correctly and be updated via responses to the events.  Refresh again at they are back to 0.
     
    Adam, I've viewed the samples and I think you need to try testing it with the event attachment occuring onload vs. the button.  It appears the onload code is running before the CE control is fully rendered and setup.
     
    <script language="javascript">
          // get the cute editor instance
          var editor1 = document.getElementById('<%=RemarksEditor.ClientID%>');
       function CE_attachEvent()
       {
        // get the cute editor instance
        if(editor1.attachEvent)
         editor1.attachEvent('onclick',CountWords);
        else if(editdoc.addEventListener)
         editor1.addEventListener('onclick',CountWords,true);
       
        //Get the editor content 
        var editdoc=editor1.GetDocument();
        // attach Event
        if(editdoc.attachEvent)
         editdoc.attachEvent('onkeypress',CountWords);
        else if(editdoc.addEventListener)
         editdoc.addEventListener('keypress',CountWords,true);
        
         CountWords();
       }  
      
       function CE_detachEvent()
       {
        // get the cute editor instance
        //var editor1 = document.getElementById('<%=RemarksEditor.ClientID%>');
       
        //Get the editor content 
        var editdoc=editor1.GetDocument();
        // detach Event
        if(editdoc.detachEvent)
         editdoc.detachEvent('onkeypress',CountWords);
        else if(editdoc.removeEventListener)
         editdoc.removeEventListener('keypress',CountWords,true);
       }
         
        //as page loads, attach it:
        //tried to do this with Page.RegisterStartupScript, but it also, only sometimes works
        //with this inline version, it works 1 of 8 times.
       CE_attachEvent();
       
       
          function CountWords()
          {
                // get the cute editor instance
                document.getElementById('RemarksEditorCount').innerHTML = 'Character Count = ' + editor1.getHTML().length;
          }
    </script>
  •  03-12-2006, 3:02 PM 17047 in reply to 16878

    Re: JavaScript content count is unreliable with attachEvent for keypress/onclick

    HotAir,
     
    Change your code to:
     
    <script language="javascript">
          // get the cute editor instance
          var editor1 = document.getElementById('<%=RemarksEditor.ClientID%>');
       function CE_attachEvent()
       {
        // get the cute editor instance
        if(editor1.attachEvent)
         editor1.attachEvent('onclick',CountWords);
        else if(editdoc.addEventListener)
         editor1.addEventListener('onclick',CountWords,true);
       
        //Get the editor content 
        var editdoc=editor1.GetDocument();
        // attach Event
        if(editdoc.attachEvent)
         editdoc.attachEvent('onkeypress',CountWords);
        else if(editdoc.addEventListener)
         editdoc.addEventListener('keypress',CountWords,true);
        
         CountWords();
       }  
      
       function CE_detachEvent()
       {
        // get the cute editor instance
        //var editor1 = document.getElementById('<%=RemarksEditor.ClientID%>');
       
        //Get the editor content 
        var editdoc=editor1.GetDocument();
        // detach Event
        if(editdoc.detachEvent)
         editdoc.detachEvent('onkeypress',CountWords);
        else if(editdoc.removeEventListener)
         editdoc.removeEventListener('keypress',CountWords,true);
       }
         
        //as page loads, attach it:
        //tried to do this with Page.RegisterStartupScript, but it also, only sometimes works
        //with this inline version, it works 1 of 8 times.
       CE_attachEvent();
       setTimeout(CE_attachEvent,1000);
       
          function CountWords()
          {
                // get the cute editor instance
                document.getElementById('RemarksEditorCount').innerHTML = 'Character Count = ' + editor1.getHTML().length;
          }
    </script>
     
     

    asp.net Chat http://cutesoft.net/ASP.NET+Chat/default.aspx
    Web Messenger: http://cutesoft.net/Web-Messenger/default.aspx
    asp.net wysiwyg editor: http://cutesoft.net/ASP.NET+WYSIWYG+Editor/default.aspx
    asp wysiwyg html editor: http://cutesoft.net/ASP
    asp.net Image Gallery: http://cutesoft.net/ASP.NET+Image+Gallery/default.aspx
    Live Support: http://cutesoft.net/live-support/default.aspx

View as RSS news feed in XML