Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6635771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:05:52+00:00 2026-05-25T23:05:52+00:00

Let’s say I have a page that loads pages dynamically. As each page loads

  • 0

Let’s say I have a page that loads pages dynamically. As each page loads into the DOM, events for the elements in that page are added.

If the user loads another page, the elements loaded previously will be removed from the DOM. Naturally, because the elements themselves no longer exist, any events mapped to those elements cease to function.

However, are they also removed? Or are they sitting in the user’s memory, taking up space?

Follow-up:
Were a function defined as such:

var event = $('foobar').addEvent('click', function() {
    alert(1);
});

One could easily remove the event with event = null (or so I’d assume!)…

but what if the event were not saved to a local variable?

$('foobar').addEvent('click', function() {
    alert(1);
});

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:05:53+00:00Added an answer on May 25, 2026 at 11:05 pm

    first of all. what? this makes no sense:

    var event = $('foobar').addEvent('click', function() {
        alert(1);
    });
    

    it does not save the event into a local variable as you seem to think. it saves a reference to the foobar element object into the event variable – most mootools element methods will return this for chaining, which is the element itself and not the result of the method (unless it’s a getter like ‘.getStyle’).

    it then depends on how you get rid of the element what happens next. first off, element.destroy, found here: https://github.com/mootools/mootools-core/blob/master/Source/Element/Element.js#L728

    it will remove the element from the dom and from memory, and empty it in a safe way. it will be reliant on the browser’s GC to clean up once it’s gone, mootools won’t do any spectacular GC for you for the element itself but it does run the special clean function on the child nodes as well: var children = clean(this).getElementsByTagName('*');.

    the clean method also gets rid of any event handlers and storage attached to the child elements of the div.

    THEN. events added by mootools go into element storage. Element storage is in an object behind a closure which the element proto uses. To test it, we will re-implement it and make it puncturable (a global object called storage) so we can check what happens to the reference after the parent is gone:

    http://jsfiddle.net/dimitar/DQ8JU/

    (function() {
        var storage = this.storage = {}; // make it puncturable
    
        var get = function(uid){
            return (storage[uid] || (storage[uid] = {}));
        };
    
        Element.implement({
           retrieve: function(property, dflt){
                var storage = get($uid(this)), prop = storage[property];
                if (dflt != null && prop == null) prop = storage[property] = dflt;
                return prop != null ? prop : null;
            },
    
            store: function(property, value){
                var storage = get($uid(this));
                storage[property] = value;
                return this;
            },
    
            eliminate: function(property){
                var storage = get($uid(this));
                delete storage[property];
                return this;
            }
    
    
        });
    
    })();
    
    // read it.
    var link = document.getElement("a");
    var uid = link.uid; // will reference the mootools unique id for it
    
    // add an event handler
    link.addEvent("click", function(e) {
        console.log("hi");
        this.destroy();
        // see what's left in storage for that element.
        console.log(storage[uid]);
    
        // storage should be empty.
        console.log(storage);
    });
    
    link.getFirst().addEvent("mouseenter", function() {
       console.log("over");
    });
    
    // check to see if it is there via element storage API.
    console.log(link.retrieve("events").click);
    
    // check to see if it's there via our puncture
    console.log(this.storage[uid]);
    
    // see all events in storage, inc child element:
    console.info(this.storage);
    

    what all this proves is, mootools cleans up all you need cleaned. as long as you don’t use any inline onclick= stuff on elements you work with, you’re going to be fine. Between mootools’ garbage collection and the browser, you are well covered. just be aware you can stack up multiple events on a single element if the callbacks are anonymous.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have some text as follows: do this, do that, then this,
Let's say that I have a set of relations that looks like this: relations
Let's say I have a javascript array with a bunch of elements (anywhere from
Let's say I have multiple requirements for a password. The first is that the
Let's say that I have a date in R and it's formatted as follows.
let's say that I have string: s = Tuple: and Tuple (stored in a
Let's say for example, I have the following javascript function that returns a boolean:
Let's say I'm building a data access layer for an application. Typically I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.