Search code examples
javascriptjqueryeventsbindlive

if jquery live event code has been removed, do i still have to unbind it?


im loading pages with ajax and also loading pages javascript code with it.

i wanted to know, if i remove page javascript code, which has jquery live bind event on it, will it kill the binding or do i still have to call $(this).die(); function?

jquery live bind event

jQuery(function($) {

    $('.showNavLink').live('click', function() {

    });
});

Ajax Page Flow

  • home page is loaded
  • home page javascript is loaded
  • user requested to load new page (on click)
  • removing home page code with javascript
  • removing javascript code
  • loading new page... (starts the cycle with first step).

Solution

  • $.live is deprecated http://api.jquery.com/live/. But if you are using it, you should remove it. I'm assuming you're never refreshing the page. Just removing script tags doesn't undo everything that was done when the script was run.

    The new way is

    $(document).on('click', '.showNavLink', function() {});
    

    When it's no longer needed

    $(document).off('click', '.showNavLink');