Correct way to use custom Javascript?
See original GitHub issueHey,
First of all, thanks for Barba.js it’s awesome. But I’ve a problem/question.
What is the right way to use custom Javascript? Right now I use functions, which I call ‘onEnter’. But when I change from page the previous code will still be executed. How can I only execute the code on the current page? Because I get errors in de log because elements do not exists.
`var Home = Barba.BaseView.extend({
namespace: 'home',
onEnter: function() {
/* ---------------------------------------
The new Container is ready and attached to the DOM.
--------------------------------------- */
// Add custom JS for the homepage
$.fn.addHome();
}
});
var Contact = Barba.BaseView.extend({
namespace: 'contact',
onEnter: function() {
/* ---------------------------------------
The new Container is ready and attached to the DOM.
--------------------------------------- */
// Add custom JS for the contact page
$.fn.addContact();
}
});
$.fn.addHome = function() {
// Do Something
}
$.fn.addContact = function() {
// Do Something
}`
Thanks!
Issue Analytics
- State:
- Created 6 years ago
- Comments:10
Top Results From Across the Web
How to Add Custom JavaScript to Your WordPress Site
You can add custom JavaScript to your WordPress site either by using a plugin or by editing your theme or child theme's functions.php...
Read more >How to Add Custom JavaScript to WordPress (3 Methods)
There is a way to add custom WordPress JavaScript by inserting the <script> tag directly into your header.php file. However, this method is...
Read more >Learn How to Add Custom JavaScript to WordPress the Right ...
How to Add Custom JavaScript to WordPress · Rule #1: Don't Use the WordPress Editor · Rule #2: Don't Modify Your Files Directly...
Read more >How to Add Custom JavaScript to Your WordPress Site
Wondering how to add JavaScript code to your WordPress site? This video will answer all your questions. ▻ Download creative digital assets ...
Read more >Custom JavaScript Functions - Medium
A tutorial about the intricacies of custom JavaScript functions and the different ways of declaring and using them.
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
@TK94 I figured it out. I created views and in my particular view that used the Scroll Event listener with Height and Top, I registered my event onEnter and de-registered my event onLeave.
onEnter: window.addEventListener(‘scroll’, myFunction); onLeave: window.removeEventListener(‘scroll’, myFunction);
OR (jQuery)
onEnter: $(window).on(‘scroll’, myFunction); onLeave: $(window).on(‘scroll’, myFunction);
you could wrap all of your custom javascript in a function… and then call that function once barba has finished transitioning between pages.