Urgent! Can't use vanilla event listeners w/ barba (using function to re-init scripts on newPageLoad)
See original GitHub issueHi,
Super close to a deadline here. I went to add event listeners & suddenly everything is going wrong. I keep getting “Cannot read property ‘addEventListener’ of null” on any event listeners. jQuery works fine however… although I really can’t afford to add the library just for this case.
I’m assuming it’s just firing before they are available to attach to, so I’ve tried all sorts of load listeners & events… no cigar just yet.
Any idea what I can do? Thank you in advance 😃
window.addEventListener("load", function () {
Barba.Dispatcher.on('transitionCompleted', function(currentStatus, oldStatus, container) {
reInitializeScripts();
});
}
function reInitializeScripts() {
let nextButton = document.querySelector(".swipe-button-next"),
backButton = document.querySelector(".swipe-button-prev"),
nextButtonText = document.querySelector('.swiper-button-next-text'),
backButtonText = document.querySelector('.swiper-button-back-text'),
nextButtonStroke = document.querySelector('.swiper-btn-next-stroke-clr'),
backButtonStroke = document.querySelector('.swiper-btn-back-stroke-clr');
nextButtonArrow = document.querySelector('.fuego-button-arrow-next'),
backButtonArrow = document.querySelector('.fuego-button-arrow-back');
TweenMax.set([nextButtonStroke, backButtonStroke], {drawSVG: "0% 0%"});
let nextButtonTL = new TimelineMax({paused:true, reversed:true}),
prevButtonTL = new TimelineMax({paused:true, reversed:true});
nextButton.addEventListener('mouseover', function() {
nextButtonTL.to(nextButtonStroke, 0.52, {
drawSVG:"100% 0%"
}, 0);
nextButtonTL.to(nextButtonText, 0.52, {
color:"#DA8978"
}, 0);
nextButtonTL.to(nextButtonArrow, 0.52, {fill:"#DA8978"}, 0);
nextButtonTL.play();
});
backButton.addEventListener('mouseover', function() {
prevButtonTL.to(backButtonStroke, 0.52, {
drawSVG:"0% 100%"
}, 0);
prevButtonTL.to(backButtonText, 0.52, {
color:"#DA8978"
}, 0);
prevButtonTL.to(backButtonArrow, 0.52, {fill:"#DA8978"}, 0);
prevButtonTL.play();
});
nextButton.addEventListener('mouseout', function() {
nextButtonTL.reverse();
});
backButton.addEventListener('mouseout', function() {
prevButtonTL.reverse();
});
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:14 (3 by maintainers)
Top Results From Across the Web
Urgent! Can't use vanilla event listeners w/ barba ... - GitHub
Urgent! Can't use vanilla event listeners w/ barba (using function to re-init scripts on newPageLoad) #350.
Read more >barba js v2 reinit main.js script - Stack Overflow
I figured it out. There is barba hook "after" that we can use in the transition script :) barba.hooks.after(() => { const bottomDOM ......
Read more >Why the vanilla JS matches() method won't work with event ...
I often use event delegation with click event listeners in my scripts, with the matches() method to check which element was clicked.
Read more >Lightbox Gone Wild! By Chris Campbell - Particletree
Our demo illustrates a few of the possibilities available to a developer using our Lightbox Gone Wild Script. Just click on the links...
Read more >Professional
Using Client-Side Validation Functions. 259. Using Dojo Validation Widgets. 260. Validation in Practice: Server-Side Validation of the Completed Form.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I guess that you are using the same script on every page, listening the transitionCompleted event, but not every page contains all the elements that are initialized in your script.
For example, if a page does not contain the ‘.swipe-button-next’ element, the variable will be null and therefore the addEventListener method will thrown an error. You have to test it before like this:
Otherwise you can run specific code based on different namespace, have a look at the View documentation here: http://barbajs.org/views.html
In the same way you have to destroy everything when the old container is removed, to prevent memory leaks or duplicated listeners.
Do not get me wrong, but Barba has nothing to do with this problems. These are common patterns of a Single Page Application and you should understand the plugins/modules lifecycle better before using them.
Glad to hear! 👍