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.

Is it possible to lazy load braintree.js on a web page?

See original GitHub issue

I want to only load braintree.js when it is needed, not on page load. I have tried using jQuery’s $.getScript like so:

$.getScript('https://js.braintreegateway.com/v2/braintree.js')
  .done(function () {
    client = new braintree.api.Client({
      clientToken: config.clientToken
    });
  });

However, loading it this way does not propagate the braintree object onto window, so braintree does not resolve. I read the documentation and came across this statement:

If you include braintree.js in a <script> tag, the braintree object will be available on the global namespace.

As such, I have tried dynamically creating a script element and adding it to the page, like so:

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://js.braintreegateway.com/v2/braintree.js';
document.getElementsByTagName('head')[0].appendChild(script);
// I've also tried: document.body.appendChild(script)

The script is fetched and shows up in my loaded scripts, but the braintree object still is not added to window.

Is there any way to lazy load braintree.js? I really do not want to load a 50kb (gzipped) script unless the user actually needs it, which isn’t always the case.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
EvanHahncommented, Dec 4, 2015

I tested the first snippet you posted with jQuery 1.11.2 and it worked for me; window.braintree was defined after running that. What version of jQuery are you using?

The second snippet you posted should also work so long as you wait for the script to load before you start using braintree:

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://js.braintreegateway.com/v2/braintree.js';
script.onload = function () {
  client = new braintree.api.Client({
    clientToken: config.clientToken
  });
};
document.getElementsByTagName('head')[0].appendChild(script);

Hope this helps!

0reactions
EvanHahncommented, Dec 10, 2015

Let us know if you have more problems!

Read more comments on GitHub >

github_iconTop Results From Across the Web

It's time to lazy-load offscreen iframes! - web.dev
Standardized lazy-loading of iframes defers offscreen iframes from being loaded until the user scrolls near them. This saves data, speeds up the ...
Read more >
Lazy loading - Web performance | MDN
Lazy loading is a strategy to identify resources as non-blocking (non-critical) and load these only when needed. It's a way to shorten the ......
Read more >
Setup | JavaScript - Braintree Developer Documentation
In a nutshell, Braintree's JavaScript SDK requires you to load Braintree.js and initialize it with some options. Once you've done that, you can...
Read more >
Lazy-Loading JavaScript for High-Speed Webpage Performance
Notably, Google Chrome 76 and higher support lazy loading natively with a loading attribute that enables you to defer loading of offscreen ...
Read more >
JavaScript v2 | Best Practices and Troubleshooting
In certain scenarios you may need to remove your Braintree.js integration. This is common in single page applications, modal flows, and other situations...
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