Results 1 to 4 of 4

Thread: Jquery functions sticking in browser

  1. #1
    Join Date
    May 2010
    Location
    Sacramento, CA
    Posts
    91
    Thanks
    23
    Thanked 2 Times in 2 Posts

    Question Jquery functions sticking in browser

    Hey all,

    I have a page that uses $.load() to bring a page into a div, within THAT div another page is brought in using $.load(). What i've noticed is if i load a page with javascript functions in the script tags, then leave that page to another, you can call the functions from a page that does not have them in script tags, in other words, it's as if they are sticking to the browser window until i refresh the entire page. I do not understand this or why it's happening. It's not causing any problems for me only the concern that the user experience will get bogged down further and further the more pages they visit because the javascript is sticking.

    Any help or explanation is greatly appreciated~

    -Cory

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Once something becomes part of the DOM and/or is loaded into the javascript event buffer, it remains there unless explicitly removed. Since I'm not sure what exact function you're talking about, I'm not sure how you would do that in this specific case. But for example, if you did:

    Code:
    var $link1 = $('#someid').click(function(){alert('here');});
    and later the element with someid is taken away, you could still do:

    Code:
    $link1.trigger('click');
    and it would fire the alert. But you could at any time do (jQuery less than version 1.7x):

    Code:
    $link1.unbind('click');
    or for version 1.7+:

    Code:
    $link1.off('click');
    and the reference would no longer exist.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    crobinson42 (09-09-2012)

  4. #3
    Join Date
    May 2010
    Location
    Sacramento, CA
    Posts
    91
    Thanks
    23
    Thanked 2 Times in 2 Posts

    Default

    That makes a little sense, here's an example of my concern:

    My index page loads a page using jquery into a div element on the page -the page being loaded into the div contains this code:
    Code:
    <script type="text/javascript">
    function editTerritory(id){
    	$('#'+id).load('modules/sales/list/territory_list.edit.php?id='+id,function(){
    		$('.editRow').hide();	
    	});
    	
    }
    function deleteTerritory(id){
    	$.load('modules/sales/list/territory_list.delete.php?id='+id,function(){
    	$('#'+id).remove();	
    	});
    }
    $(document).ready(function(){
    	$('#editTerritoryForm').ajaxForm(function(){
    		reloadTerritory();
    	});
    });
    function reloadTerritory(){
    	$('#salesContent').load('modules/sales/list/territory_list.php');	
    }
    </script>
    Then using the menu bar on the index page, i click a link to load a different page into the same div element (replacing it with the new html), the new page contains this code:
    Code:
    <script type="text/javascript">
    $(document).ready(function(){
    	$('#searchSales').keyup(function(){
    		$('#searchResult').html('<img src="images/loading2.gif" />').load('modules/sales/main.search_sales.php?search='+this.value);
    	});
    });
    function viewFile(fileId){
    $('#salesContent').load('modules/sales/file/view_file.php?id='+fileId);
    }
    </script>
    Now when i've loaded completely new content into this div element on the page, i can still call javascript functions from the previous content that was loaded into this same div.

    My question is: How do i go about clearing out the 'javascript event buffer' between pages to prevent the uneccesarry build up of javascript code in the DOM or event buffer?

  5. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    With one possible exception, you don't need to. Each time you define a function like:

    Code:
    function editTerritory(id){
    	$('#'+id).load('modules/sales/list/territory_list.edit.php?id='+id,function(){
    		$('.editRow').hide();	
    	});
    	
    }
    it overwrites any previous function by that same name. So you aren't building up tons of references.

    The one possible exception is here:

    Code:
    $(document).ready(function(){
    	$('#searchSales').keyup(function(){
    		$('#searchResult').html('<img src="images/loading2.gif" />').load('modules/sales/main.search_sales.php?search='+this.value);
    	});
    });
    But that element is gone if and when you do that again. To prevent a problem though that might arise, you could do this before each time you load new content (for jq less than 1.7):

    Code:
    $('#searchSales').unbind('keyup');
    For jq 1.7+:

    Code:
    $('#searchSales').off('keyup');
    Most browsers will garbage collect that anyway and throw it out though. Some might build up the orphan references and eventually run out of memory if its done like 200, 2000, maybe 20000 or more times before the page is reloaded. Some might even hold on to the references after the page is dismissed. But browsers like that will have problems with a lot of other code and their owners will be used to closing them periodically to free up memory.

    I honestly wouldn't worry about any of it unless you find something isn't working as expected and that can be traced back to multiple references to the same element.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Resolved jQuery loop functions
    By ggalan in forum JavaScript
    Replies: 13
    Last Post: 09-18-2011, 07:49 PM
  2. all levels sticking
    By discdemo in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 09-28-2009, 03:50 PM
  3. Glossy Accordion Menu Keeps Sticking
    By gerdydog in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 08-08-2009, 12:57 PM
  4. Replies: 1
    Last Post: 08-25-2008, 03:17 PM
  5. Dynamic Ajax Content, but with browser functions?
    By Quizosde in forum Dynamic Drive scripts help
    Replies: 4
    Last Post: 08-18-2008, 07:43 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •