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 8914727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:47:47+00:00 2026-06-15T04:47:47+00:00

I recently read a tutorial on cross-browser event handling… Cross-Browser Event Handling… I’ve adapted

  • 0

I recently read a tutorial on cross-browser event handling… Cross-Browser Event Handling…

I’ve adapted some practises from the tutorial to a generalised function that’s floating around like so…

function isHostMethod(object, property) {
    var type = typeof object[property];
    return type === "function" || (type === "object" && !! object[property]) || type === "unknown";
}

var events = {
    add: (function() {
        if (isHostMethod(this, 'addEventListener')) {
            return function(element, type, handler) {
                element.addEventListener(type, handler, false);
            };
        } else if (isHostMethod(this, 'attachEvent')) {
            return function(element, type, handler) {
                element.attachEvent('on' + type, function() {
                    handler.call(element, window.event);
                });
            };
        } else {
            return function(element, type, handler) {
                element['on' + type] = handler;
            };
        }
    }()),
    remove: (function() {
        if (isHostMethod(this, 'removeEventListener')) {
            return function(element, type, handler) {
                element.removeEventListener(type, handler, false);
            };
        } else if (isHostMethod(this, 'detachEvent')) {
            return function(element, type, handler) {
                element.detachEvent('on' + type, function() {
                    handler.call(element, window.event);
                });
            };
        } else {
            return function(element, type, handler) {
                element['on' + type] = null;
           };
       }
    }())
};

usage:

var img_wrap = document.getElementById('img_wrap'),
    img = img_wrap.getElementsByTagName('img'),
    img_amount = img.length;

function do_stuff() {
    //
    //do stuff - update preloaded percentage, etc. 
    //
    events.remove(this, 'load', do_stuff);
}

for (var i = 0; i < img_amount; i += 1) {
    events.add(img[i], 'load', do_stuff);
}

This is what I’m curious about…
1. Is the ‘isHostMethod’ function necessary?
2. The tutorial warns about memory leaks and outlines an addition function for avoiding them using ‘unique ids’ as a reference to each element… This hasn’t been included… Maybe I’m overseeing something but I don’t think it’s necessary in my case… OR should I be concerned?

jsfiddle here
warning: the images are huge, take a while to download, lots of bandwidth, etc.

  • 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-06-15T04:47:48+00:00Added an answer on June 15, 2026 at 4:47 am

    1.Is the ‘isHostMethod’ function necessary?

    Nop, unless you persist to support IE6,7 and 8. Every modern browser uses addEventListener() and removeEventListener() now.

    So I guess this is not acceptable, that’s why people uses libs like mootools and jQuery. And they will take care that for you.

    2.The tutorial warns about memory leaks and outlines an addition function for avoiding them using ‘unique ids’ as a reference to each element… This hasn’t been included… Maybe I’m overseeing something but I don’t think it’s necessary in my case or should I be concerned about memory leaks?

    It’s depends. If you’re going to build a Gmail, Stackoverflow, FB, G+ or Twitter, memory leak do matter a lot, as the pages on these sites barely refresh, and will live for a long time.

    But if you just want to make a web page, attach some events and people will click on a link and move to next. As the context will be freed when they move on, even you do leak some memory, it won’t cause any trouble (though I think you should try to avoid it, but it’s not a must.)

    And here is a tutorial from IBM on how to avoid memory leak (which is a little out-of-date, and some patterns are no longer leak memory.) It may help you.

    And in practice, you can always profile memory usage using Chrome’s inspector, which is a very powerful tool.

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

Sidebar

Related Questions

I recently read a tutorial on CSS browser feature detection... The end product was
I recently started learning Emacs . I went through the tutorial, read some introductory
I recently read in this tutorial that certain jQuery leaks are trackable through the
I recently read a jQuery tutorial about best practices. I usually run all my
Recently I read a nice tutorial How to Authenticate Users With Twitter OAuth even
Recently, I read this article: http://download.oracle.com/javase/tutorial/extra/generics/wildcards.html My question is, instead of creating a method
I recently read about a new Google-code hosted (open source) project from Google that
Firstly , I am a freshmen to outlook add-in development,Recently I read some learning
I recently read Object Oriented Exception Handling in Perl Perl.com article. Is there any
I recently read an article about password hashing . How are MD5 or SHA1

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.