Search code examples
javascriptjquerymixpanel

How can I prevent an external JS file from breaking my site?


I'm currently using Mixpanel on my site. For some reasons they go down from time to time and that disables my site from loading.

Is there a way I can make the load async or some other idea where it doesn't bring down my site when they have downtime?

Here's the code:

<script>
    var mp_protocol = (("https:" == document.location.protocol) ? "https://" : "http://");
    document.write(unescape("%3Cscript src='" + mp_protocol + "api.mixpanel.com/site_media/js/api/mixpanel.js' type='text/javascript'%3E%3C/script%3E"));
</script>

Thanks


Solution

  • Do not document.write it to the page, append it.

    <script>
    $( function(){
        var mp_protocol = (("https:" == document.location.protocol) ? "https://" : "http://");
        jQuery.getScript(unescape(mp_protocol + "api.mixpanel.com/site_media/js/api/mixpanel.js"));
    });
    </script>