Click here to Skip to main content
16,022,259 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My javascript loads and runs fine on initial page load. After a postback, it no longer fires any of the alerts I've placed in functions to tell whether they've run. The alert "Page Reload" never pops up at all, but the alert just above it does. The purpose of this script is to count down keystrokes until it hits 0. Can someone please help?

<script type="text/javascript">

  document.addEventListener('DOMContentLoaded', function () {
      const textArea2 = document.getElementById('TextBox_Follow_Up_Answer2');
      const label1 = document.getElementById('Label_Answer_Char_Count');
      const label2 = document.getElementById('Label_Answer_Char_Count2');
      const labelRemaining1 = document.getElementById('Label_Answer_Char_Remaining');
      const labelRemaining2 = document.getElementById('Label_Answer_Char_Remaining2');

      // Add input event listener for text area
      textArea2.addEventListener('input', function () {
          textCounter(textArea2, 3000, label1, labelRemaining1);
          textCounter2(textArea2, 865, label2, labelRemaining2);
          alert("Event Listener - DOMContentLoaded");
      });
  });

     function textCounter(field, maxlimit, label, label2) {
         if (field.value.length > maxlimit)
            field.value = field.value.substring(0, maxlimit);
         else {
            label.innerHTML = maxlimit - field.value.length;
         }

         if (field.value.length > maxlimit - 500) {
            label.style.color = "#FF0000";
            label2.style.color = "#FF0000";
         }
         else {
            label.style.color = "#000000";
            label2.style.color = "#000000";
         }
       alert("textCounter Fired");
     }

     function textCounter2(field, maxlimit, label, label2) {
         if (field.value.length > maxlimit)
         //  field.value = field.value.substring(0, maxlimit);
            field.value = field.value;
         else {
            label.innerHTML = maxlimit - field.value.length;
         }

         if (field.value.length > maxlimit - 200) {
            label.style.color = "#FF0000";
            label2.style.color = "#FF0000";
         }
         else {
            label.style.color = "#000000";
            label2.style.color = "#000000";
         }
       alert("textCounter2 Fired");
     }

     $(document).ready(function () {
         alert("Page Initial Load");
         // this supposedly works for initial page load only
         textCounter(textArea2, 3000, label1, labelRemaining1); 
         textCounter2(textArea2, 865, label2, labelRemaining2);
     });
            
     alert(typeof (Sys));

     if (typeof (Sys) !== 'undefined') {
          alert("Sys");
         Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function () {
              alert("Page Reload");
              // this supposedly works after postback
              textCounter(textArea2, 3000, label1, labelRemaining1);          
              textCounter2(textArea2, 865, label2, labelRemaining2);
          });
     }

        </script>


What I have tried:

I tried adding the two functions at the end, which I saw on another similar question. It didn't work for me, and as I said above, the Javascript code never runs the "Page Reload" alert either on initial load or after postback.

After a little more research, it appears add_endRequest is only available for asynchronous postback. My page has no update panels, so I may need to explore a different avenue.
Posted
Updated 12-Sep-24 3:25am
v3

1 solution

When the page is reloaded using an UpdatePanel, the DOM is replaced with the data returned by an AJAX request. The references you have cached via document.getElementById are no longer valid; they are referring to orphaned elements which are no longer part of the DOM.

You can either replace those references and re-add the event handlers when the page reloads, or use delegated event handlers[^] to handle the events. Event delegation is probably the safest, since it avoids leaking any references to orphaned DOM elements:
JavaScript
function textCounter(field, maxlimit, label, label2) { ... }
function textCounter2(field, maxlimit, label, label2) { ... }

function textCounterFollowUpAnswer2(field) {
    const label1 = document.getElementById('Label_Answer_Char_Count');
    const label2 = document.getElementById('Label_Answer_Char_Count2');
    const labelRemaining1 = document.getElementById('Label_Answer_Char_Remaining');
    const labelRemaining2 = document.getElementById('Label_Answer_Char_Remaining2');
    
    textCounter(target, 3000, label1, labelRemaining1);
    textCounter2(target, 865, label2, labelRemaining2);
}

document.addEventListener('input', function (e) {
    const target = e.target;
    if (target.id === 'TextBox_Follow_Up_Answer2') { 
        textCounterFollowUpAnswer2(target);
        alert("Event Listener");
    }
});

document.addEventListener('DOMContentLoaded', function () {
    alert("Page Initial Load");
    const textArea2 = document.getElementById('TextBox_Follow_Up_Answer2');
    textCounterFollowUpAnswer2(textArea2);
});

if (typeof (Sys) !== 'undefined') {
    alert("Sys");
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function () {
        alert("Page Reload");
        const textArea2 = document.getElementById('TextBox_Follow_Up_Answer2');
        textCounterFollowUpAnswer2(textArea2);
    });
}
 
Share this answer
 
Comments
John Chief 12-Sep-24 11:31am    
You saw that my page doesn't have an Update Panel? Will this solution still work?
Richard Deeming 12-Sep-24 11:42am    
The PageRequestManager code suggests you're using something that's replacing the page contents via an AJAX request. If that's the case, then the answer still stands. :)
Pete O'Hanlon 13-Sep-24 6:59am    
If I could offer more than a 5, I would.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900