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.

Correct way to use custom Javascript?

See original GitHub issue

Hey,

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:closed
  • Created 6 years ago
  • Comments:10

github_iconTop GitHub Comments

2reactions
bradleyDouglascommented, Mar 16, 2018

@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);

1reaction
magicsponcommented, Mar 14, 2018

you could wrap all of your custom javascript in a function… and then call that function once barba has finished transitioning between pages.

Read more comments on GitHub >

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

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