ReferenceError:“未定义twttr”甚至在使用twttr.ready()时 [英] ReferenceError: "twttr is not defined" even while using twttr.ready()

查看:105
本文介绍了ReferenceError:“未定义twttr”甚至在使用twttr.ready()时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firebug控制台抛出错误。它声明我在尝试用于跟踪社交事件的代码是在//platform.twitter.com/widgets.js异步加载完成之前使用的。

Firebug console throws an error. It states that the code which I'm trying to use for tracking social events is used before //platform.twitter.com/widgets.js has finished loading asynchronously.


ReferenceError:未定义twttr twttr.ready(函数(twttr){

ReferenceError: twttr is not defined twttr.ready(function (twttr) {

但是,我关注了Twitter文档( https://dev.twitter.com/web/javascript/events ),并将其包裹在twttr.ready()周围,与Facebook事件相同。

However, I followed Twitter documentation ( https://dev.twitter.com/web/javascript/events ), and wrapped it around twttr.ready(), in the same way one does with Facebook events.

  <script type="text/javascript">   //load social sharing buttons async
    (function(w, d, s) {
      function go(){
        var js, fjs = d.getElementsByTagName(s)[0], load = function(url, id) {
          if (d.getElementById(id)) {return;}
          js = d.createElement(s); js.src = url; js.id = id;
          fjs.parentNode.insertBefore(js, fjs);
        };
        load('//connect.facebook.net/en_US/all.js#appId=1111111111111111&xfbml=1', 'fbjssdk');
        load('https://apis.google.com/js/plusone.js', 'gplus1js');
        load('//platform.twitter.com/widgets.js', 'tweetjs');
      }
      if (w.addEventListener) { w.addEventListener("load", go, false); }
      else if (w.attachEvent) { w.attachEvent("onload",go); }
    }(window, document, 'script'));
</script>  

<script type="text/javascript">
        window.fbAsyncInit = function() {
              FB.Event.subscribe('edge.create', function(targetUrl) {
              ga('send', 'social', 'facebook', 'like', targetUrl);
            });
              FB.Event.subscribe('edge.remove', function(targetUrl) {
              ga('send', 'social', 'facebook', 'unlike', targetUrl);
            });
        }
        twttr.ready(function (twttr) {
         twttr.events.bind('tweet', function(e){
          if(!e) return;
          ga('send', 'social', 'twitter', 'tweet', theURL);
         })
        });
    </script>

任何提示?

推荐答案

twttr 似乎尚未初始化。确保脚本( // platform.twitter.com/widgets.js )在您拥有的代码块之前加载。

It appears that twttr is not initialized yet. Make sure the script (//platform.twitter.com/widgets.js) is loading before the block of code you have.

这就是你未定义的原因(twttr)。

This is the reason you're getting undefined (twttr).

看到你的编辑后,很清楚发生了什么。您的脚本在页面加载后附加头部并加载脚本。在您执行代码之后,这些代码依赖于您注入头部并仍在加载的内容,这就是 twttr 尚未初始化的原因。

After seeing your edits, it's quite clear what is happening. Your scripts are appending the head and loading the scripts after the page is loading. Right after you're executing code that depends on the stuff you've injected into the head and are still loading which is why twttr is not initialized yet.

请尝试以下代码块:

window.addEventListener("load", function() {
    window.fbAsyncInit = function() {
        FB.Event.subscribe('edge.create', function(targetUrl) {
            ga('send', 'social', 'facebook', 'like', targetUrl);
        });
        FB.Event.subscribe('edge.remove', function(targetUrl) {
            ga('send', 'social', 'facebook', 'unlike', targetUrl);
        });
    }

    document.getElementById('tweetjs').addEventListener('load', function() {
        twttr.ready(function (twttr) {
            twttr.events.bind('tweet', function(e){
                if(!e) return;
                ga('send', 'social', 'twitter', 'tweet', theURL);
            })
        });
    }, false);
}, false);

如果您想要跨浏览器支持,您可能需要按照他们在功能中所做的操作来检查首先是window.addEventListener,然后是旧浏览器的window.attachEvent。

If you want cross browser support, you may want to follow what they did in their function to check for window.addEventListener first, and then fall back to window.attachEvent for older browsers.

这篇关于ReferenceError:“未定义twttr”甚至在使用twttr.ready()时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆