Avatar of sherbug1015
sherbug1015
Flag for United States of America asked on

Jquery won't fire after postback

I have a jquery where we are trying to track what documents our users are viewing in our Document Library.  Users can upload a document and there is a pagination widget as well.

This is the jquery

$(document).ready(function(){  //
  $(".FileCabinet a").click(function () {
                    //$(document).on("click", "a", function() {
       
  if (! $(this).attr("href") ) {
     
     return;
  }
  else {  
      var href = $(this).attr("href");
      var mystring = href.replace(/.*\?/,'');  //remove everything before the ?
      var mystring1 = mystring.replace(/\&.*/,''); //remove the hash key
      var mystring2 = mystring1.replace(/[^\d\.]/g, ''); // remove non-numerics
  }  
   
alert(href);
   
  $.ajax({
    type: "POST",
    url: "/CMSPages/WebService.asmx/WhoClicked",
    data: JSON.stringify({ DocumentID: mystring2}),
    contentType: "application/json",
    success: function (data) {
      // Do something on sucess
    },
   
    error: function(jqXHR, status, errorThrown) {
      // Do something on failure
    }
  });
 
  });
 
  });
 
Whenever there is a partial postback, the jquery does not fire anymore.  I am assuming it has something to do with the document ready function.  Is there a workaround for this.  In other words how do I make this script fire on each time a document link is clicked.
jQueryJavaScript

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
Julian Hansen

This could be due to the fact that the a element click is not being stopped and the page is refreshing causing the post back not to work.

$(".FileCabinet a").click(function () { 

Open in new window


Try this

$(".FileCabinet a").click(function (e) { 
  e.preventDefault();

Open in new window

sherbug1015

ASKER
This does not help.  I read that the document ready function fires only once and if there is some kind of postback then the script won't rerun

Is there some alternative to the document ready function that will fire every time?

Thanks.
Julian Hansen

I read that the document ready function fires only once
Correct
and if there is some kind of postback then the script won't rerun
Maybe you do not understand exactly how javascript events work.
The document.ready (windows load) event fires when the page has finished loading. Within your document.ready function you do your initialisation AND you setup your event handlers.

This code
 $(".FileCabinet a").click(function () { 

Open in new window

sets up an event handler - it does not run until the element it is bound to is triggered.

What this code does is bind the function to the click event handler for every instance of <a> within elements with class .FileCabinet.

This code will run EVERY time one of those <a> elements is clicked.

But you need to remember that an <a> element has a default behaviour which is to navigate to the link in the href attribute. If you don't override this behaviour then when your click event fires on the <a> it is going to result in the code partially running followed by a navigation to the href link - which is potentially going to break the code.

Hence the recommendation to put the e.preventDefault() into the event handler to prevent the default behavour from happening.


Is there some alternative to the document ready function that will fire every time?
This is a non-question. As discussed above the document ready is where you setup your event handlers which in turn fire each time the event they are bound to fires on the bound element.
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.