Link to home
Start Free TrialLog in
Avatar of sherbug1015
sherbug1015Flag 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.
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

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

Avatar of 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.
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.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial