question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Urgent! Can't use vanilla event listeners w/ barba (using function to re-init scripts on newPageLoad)

See original GitHub issue

Hi,

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:closed
  • Created 5 years ago
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
gfnoolcommented, Mar 14, 2019

I actually did not know that… awesome thank you 😃 However I’d love to know how you can make it work with vanilla. I’m gradually moving over to purely vanilla just to get better at not using libraries so

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:


var nextButton = document.querySelector(".swipe-button-next"); 

if(nextButton != null)
    nextButton.addEventListener(...);

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.

1reaction
thierrymichelcommented, Mar 14, 2019

Glad to hear! 👍

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found