Essential polyfill for the web
npm install minifillpolyfill)
this.Document, it's this.HTMLDocument
this.Window, it's this
window.Element, it's window.HTMLElement
window.Node, it's window.Element
classList
Array instance from an array-like or iterable object, usually NodeList, HTMLCollection
attachEvent API to help legacy browsers
querySelectorAll based polyfill for document/Element
createEvent or createEventObject to make HTML4 browsers as well as IE8-IE11 work properly with today's standard Event
new Event() for stuff like unsupported events types or user defined events like my.custom.event, this also works with IE8-IE11
fireEvent API on legacy browsers
matches polyfill
classList
matches to find the closest parent element that matches the selector
requestAnimationFrame and other stuff, uses the new Date().getTime() synthax to return the current time
trim eventually
forEach polyfill, executes a provided function once for each element in a Nodelist.
getComputedStyle polyfill, returns the true dimensions, spacing, or other browser supported properties
Event
write less, do more
html
`
Custom builds
You can create your own builds specific to your application bundles, but make sure to keep the same order as for the minifill.js.
* create a new file /path-to/your-file.js
* copy contents of the minifill.js
* edit out the polyfills you don't need
* run npm run custom INPUTFILE:path-to/your-file.js,OUTPUTFILE:path-to/your-build.js,FORMAT:esm,MIN:false
** INPUTFILE - allows you to specify the source file path
** OUTPUTFILE - allows you to specify the output file path
** MIN - when true, it will compress the output
** FORMAT - umd|cjs|esm and any format you specify or configure your rollup for
Examples
Class Manipulation
`javascript
// check for a class
var docHasClass = myElement.classList.contains('someClass'); // return true|false
// add a class
myElement.classList.add('someClass');
// remove a class
myElement.classList.remove('someClass');
// toggle a class
myElement.classList.toggle('someClass');
`
String / Array checks
`javascript
// indexOf
string.indexOf('looking for this'); // returns the index of 'looking for this' within the string OR -1 if not found
// or
array.indexOf(myElement); // returns the index of an element within the array OR -1 if not found
`
Get current computed style for an element
`javascript
// getComputedStyle
var elStyle = window.getComputedStyle(myElement); // returns the current computed style for myElement
var width = elStyle.width; // returns the current computed width
`
Get the exact current time
`javascript
// window.performance.now
var timeNow = window.performance.now(); // returns a number with the exact current time
`
Create Native Events
Instead of writing
`javascript
// typical triggering events these days
if ( 'createEventObject' in document ) {
myChangeEvent = document.createEventObject();
myChangeEvent.type = type;
myChangeEvent.bubbles = bubbles;
myChangeEvent.cancelable = cancelable;
} else {
myChangeEvent = document.createEvent('Event');
myChangeEvent.initEvent(type, bubbles, cancelable);
}
`
you can simply write
`javascript
// Event
var myChangeEvent = new Event('change'); // creates 'change' Event Element / Object (legacy browsers)
`
to do it all for you.
Create Custom Events
`javascript
// CustomEvent
var myCustomEvent = new CustomEvent('my.custom.event.name'); // creates 'my.custom.event.name' CustomEvent Element / Object (legacy browsers)
`
Triggering/Dispatching Events
`javascript
myElement.dispatchEvent(myChangeEvent); // dispatches the native 'change' event for myElement, defined above
myElement.dispatchEvent(myCustomEvent); // dispatches a CustomEvent event for myElement, defined above
`
Adding Event Handlers
`javascript
// addEventListener
window.addEventListener('scroll',handler,false); // adds a new handler to the window scroll event
// OR
myButton.addEventListener('click',handler,false); // adds a 'click' (or any other supported/custom event) handler for any HTML element
`
Removing Event Handlers
`javascript
// removeEventListener
window.removeEventListener('scroll',handler,false); // removes a handler bound to the window scroll event
// OR
myButton.removeEventListener('click',handler,false); // removes a handler bound to 'click' (or any other supported/custom event) handler for any HTML element
`
NOTE: if the removeEventListener call is not in the same context with addEventListener, it will produce no effect. If you would like to autoremove a handler, you would need to write your code like this:
`javascript
window.addEventListener('scroll', function handlerWrapper(e){
handler(e);
window.removeEventListener('scroll', handlerWrapper, false);
},false);
``