如何防止外部JS文件破坏我的网站?

时间:2011-09-02 00:51:47

标签: javascript jquery mixpanel

我目前正在我的网站上使用Mixpanel。由于某些原因,它们会不时地停机并禁止我的网站加载。

有没有办法可以让负载异步或其他一些想法在停机时不会导致我的网站崩溃?

以下是代码:

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

由于

1 个答案:

答案 0 :(得分:1)

不要将文档写入页面,然后附加它。

<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>
相关问题