Event Bridge
Object for cross-browser working with events using bridge pattern.
This is a simple interface for adding and removing event listeners. Unfortunately, Internet Explorer 9 and older uses non-standard methods to do that.
You should use Event
polyfill if you want the full functionality with native syntax.
How to use
Install the library via NPM:
npm install event-bridge --save
Then use in your project like this:
; // add event listenerEventBridge; // remove event listener// NOTE: you have to use reference to the same function object that you used// when you were adding the event listener... so no anonymous functions {/*...*/}EventBridge; // now the listener is activeEventBridge; // now it is not // add multiple event listeners at onceEventBridge; // handy methods for cross-browser handling of event objectsEventBridge; // add first event supported by browser - this will add 'onPopState' event in// modern browsers, but 'onHashChange' in IE9EventBridge;
Documentation
EventCallback
Callback fired by an Event.
Type: Function
Parameters
event
Event
event_object
Object for event listener. If function is used, it will be evaluated and its return value will be used.
add
Add event listeners to the object.
Parameters
object
event_object Any object that can receive event listener. (optional, defaultwindow
)events
(string | Array<string>)? Single event name or list of event names. (optional, default[]
)callback
EventCallback? Function to be called when event is fired. (optional, defaultnoop
)
once
Add event listeners to the object. After any of the events has been fired, the event listeners are removed.
Parameters
object
event_object Any object that can receive event listener. (optional, defaultwindow
)events
(string | Array<string>)? Single event name or list of event names. (optional, default[]
)callback
EventCallback? Function to be called when event is fired. (optional, defaultnoop
)
addFirstSupported
Add first supported event listener from the list, ignore the rest.
Parameters
object
event_object Any object that can receive event listener. (optional, defaultwindow
)events
Array<string>? List of event names. (optional, default[]
)callback
EventCallback? Function to be called when event is fired. (optional, defaultnoop
)
Examples
Use onPopState
in modern browsers, but onHashChange
in IE9.
;
addFirstSupportedOnce
Add first supported event listener from the list, ignore the rest. After the event has been fired, the event listener is removed.
Parameters
object
event_object Any object that can receive event listener. (optional, defaultwindow
)events
Array<string>? List of event names. (optional, default[]
)callback
EventCallback? Function to be called when event is fired. (optional, defaultnoop
)
Examples
Cross-browser listener for CSS animation end:
;
remove
Remove event listeners from the object.
Parameters
object
event_object Any object that can receive event listener. (optional, defaultwindow
)events
(string | Array<string>)? Single event name or list of event names. (optional, default[]
)callback
EventCallback? Function to be called when event is fired. (optional, defaultnoop
)
stop
Cancel the event and stop its further propagation.
Parameters
event
Event
target
Get reference to the object that dispatched the event.
Parameters
event
Event
Returns (Object | null)
sanitizeInputObject
Sanitizes input object, evaluates it if it is function.
Parameters
input
any?
Returns any
Bug reports, feature requests and contact
If you found any bugs, if you have feature requests or any questions, please, either file an issue at GitHub or send me an e-mail at mailto:riki@fczbkk.com.
License
This project is published under the MIT license. Feel free to use it in any way.