Helper functions that solve problems extended with Javascript prototype
.
Each function is designed to be used alone.
Single Node
object supported addEventListener
, removeEventListener
, dispatchEvent
. But NodeList doesn't support with that functions. document.querySelectorAll
return as NodeList
object. If you want use event ops, you should use loop terms.
Before
const liItems = document.querySelectorAll('li');
for (let i = 0; i < liItems.length; i++) {
liItems[i].addEventListener('click', (e) => console.log('Hi'));
}
document.querySelectorAll('li').addEventListener('click', (e) => console.log('Hi'))
Before
function logHi(e){
console.log('Hi');
}
const liItems = document.querySelectorAll('li');
for (let i = 0; i < liItems.length; i++) {
liItems[i].removeEventListener('click', logHi);
}
function logHi(e){
console.log('Hi');
}
document.querySelectorAll('li').removeEventListener('click', logHi)
Before
const liItems = document.querySelectorAll('li');
for (let i = 0; i < liItems.length; i++) {
liItems[i].dispatchEvent(new CustomEvent('render', {detail: {view: 'ok'}}));
}
document.querySelectorAll('li').dispatchEvent(new CustomEvent('render', {detail: {view: 'ok'}}));
Modifications for HTMLElement
.
It is custom function for HTML video
element. If you change continually src
attribute of video
element, normally you should use element.load()
function but this function always doesn't work successfully. This function solve this issue. Check video's duration time per seconds. I tested default variables succesfully working with low network connection.
Index | Paremeter | Description | Default |
---|---|---|---|
0 | callback | When load, call function | ()=>{} |
1 | tick | Delay for check per | 1000 |
2 | wait | Delay for check duration in tick |
500 |
let findDuration = 0;
videoEl.src = response.newVideoPath;
videoEl.waitUntilLoad(function(self){
findDuration = self.duration;
});
This is same the waitUntilLoad
without callback function. Returned HTMLVideoElement
object.
Index | Paremeter | Description | Default |
---|---|---|---|
0 | tick | Delay for check per | 1000 |
1 | wait | Delay for check duration in tick |
500 |
let findDuration = 0;
videoEl.src = response.newVideoPath;
findDuration = (await videoEl.waitUntilLoadAsync()).duration;
jQuery
$(document).ready(function(){
});
document.ready(function(){
});
jQuery
$("#menu").index();
document.body.index();
document.querySelector("#menu").index();
jQuery
$("#menu").is(".test");
document.body.is(".test");
document.querySelector("#menu").is(".test");
jQuery
$("#menu").offset();
document.body.offset();
document.querySelector("#menu").offset();
jQuery
$("#menu").siblings();
document.body.siblings();
document.querySelector("#menu").siblings();