John
John

Reputation: 178

Load third party javascript last

I am using a third party script from coinwidget.com on a website. However, I am finding that this particular script takes rather long to load and delays some of the other things going on in the website. How to I make the following load last...

<script src="http://coinwidget.com/widget/coin.js"></script>
<script>
CoinWidgetCom.go({
wallet_address: "773ce37f-fa57-4946-a1f8-d3e3e4a87290"
, currency: "bitcoin"
, counter: "count"
, alignment: "bl"
, qrcode: true
, auto_show: true
, lbl_button: "Donate"
, lbl_address: "My Bitcoin Address:"
, lbl_count: "donations"
, lbl_amount: "BTC"
});
</script>

Upvotes: 0

Views: 267

Answers (5)

jasonhooten
jasonhooten

Reputation: 46

I'd make the user experience better by putting a place holder on your page where 'CoinWidget' is, move the tags to the bottom and wrap CoinWidgetCom.go() in a jQuery ready function.

<-- bottom of page -->
<script src="http://coinwidget.com/widget/coin.js"></script>
<script>
   $(function() {
       CoinWidgetCom.go({ .... });
   });
</script>

his will allow your page to display quickly since it's loads coin.js after rendering the html but makes sure CoinWidgetCom.go() doesn't get run prematurely.

Also if you're using a CDN (which you should be), why not download coin.js yourself upload it to the CDN?

Upvotes: 0

Mark Hagan
Mark Hagan

Reputation: 506

Assuming you are waiting for a div id of 'things' to populate via another script

$(window).load(function () {
    var i = setInterval(function () {
        if ($('#things').length) {
            clearInterval(i);
            // put your code here.
        }
    }, 100);
});

Upvotes: 0

kmos.w
kmos.w

Reputation: 432

Try to run the script within setInterval function so as to give room to other activities in the browser.

Upvotes: 1

Ben Dilts
Ben Dilts

Reputation: 10745

Consider using the async attribute on the third-party script tag, and then wrapping the second snippet in an onload event, like this.

Upvotes: 1

dstronczak
dstronczak

Reputation: 2464

Make sure you put the script just before the closing body tag.

Read more here

Upvotes: 1

Related Questions