Tiberiu-Ionuț Stan Tiberiu-Ionuț Stan - 25 days ago 8
Javascript Question

JavaScript `onscroll` event standalone (without frameworks)

I need a standalone JavaScript

onscroll
event handler (without frameworks such as jQuery, Prototype, mootools, etc.), which is also cross browser.

I have searched for one, I can only find thousands of examples using jQuery or Prototype.

Answer

From element.addEventListener:

    function onScrollEventHandler(ev)
    {
        alert(ev);
        //http://dev.w3.org/2006/webapi/DOM-Level-3-Events/html/DOM3-Events.html#event-type-scroll
    } 

    var el=window;

    if(el.addEventListener)
        el.addEventListener('scroll', onScrollEventHandler, false);   
    else if (el.attachEvent)
        el.attachEvent('onscroll', onScrollEventHandler);