【问题标题】:Disqus 2012 "Uncaught TypeError: Cannot call method 'getPropertyValue' of null" on Safari and ChromeDisqus 2012 Safari 和 Chrome 上的“未捕获的 TypeError:无法调用 null 的方法 'getPropertyValue'”
【发布时间】:2012-10-19 14:40:03
【问题描述】:

在我的公司,我们正在努力使用令人惊叹的 Disqus 小部件提供良好的评论体验。

我们的网站完全基于 AJAX,最多使用 requirejs、jquery 和骨干网。

我们已经在需要它的页面上添加了 Disqus 小部件。在页面更改期间,我们当然会破坏 Disqus 小部件所在的 div,然后我们再次实例化所有内容。

每个浏览器上的旧小部件一切正常。问题发生在激活 Disqus 2012 的小部件时,该小部件在 Safari 和 Chrome 上没有出现错误“Uncaught TypeError: Cannot call method 'postMessage' of null”。 Firefox 效果很好。

这里是涉及的代码:

define([
  'underscore',
  'backbone',
  'models/config',
  'models/book'
], function(_, Backbone, config) {

    App.Models.Disqus = Backbone.Model.extend({
        bookObj: null,

        initialize: function(options){
            console.log("discus.initialize()");
            var _this=this;

            _this.bookObj= new App.Models.Book({id: options.id});
            _this.bookObj.fetch({
                dataType: 'jsonp',
                success: function(model,response){
                    (typeof DISQUS == 'undefined')?_this.initDisqus():_this.resetDisqus();              }
            });

        },

        initDisqus: function(){
            console.log("discus.init()");
            disqus_shortname=config.get('DISQUS_ID');
            disqus_title = this.bookObj.get('content').title; 
            disqus_config = function (){
                // this.page.identifier = _this.id;
                // this.page.url = document.URL;
                this.language = config.get('LANG');
            };
            require(['http://'+disqus_shortname+'.disqus.com/embed.js'], function(){});
        },

        resetDisqus: function(){
            console.log("discus.reset()");
            var _this=this;
            DISQUS.reset({
                reload: true,
                config: function(){
                    this.page.identifier = _this.id;
                    this.page.url = document.URL;
                    this.page.title = _this.bookObj.get('content').title;
                    this.language = config.get('LANG');
                }
            });
        }

    });

    return App.Models.Disqus;
});

如果您需要更多信息,请随时询问。

提前致谢, 乔治

好的,伙计们,这样解决了:

define([
  'underscore',
  'backbone',
  'models/config',
  'models/book'
], function(_, Backbone, config) {

    App.Models.Disqus = Backbone.Model.extend({
        bookObj: null,

        initialize: function(options){
            console.log("discus.initialize()");
            var _this=this;
            _this.bookObj= new App.Models.Book({id: options.id});
            _this.bookObj.fetch({
                dataType: 'jsonp',
                success: function(model,response){
                    //(typeof DISQUS == 'undefined')?_this.initDisqus():_this.resetDisqus();

                    delete DISQUS;

                    disqus_shortname = config.get('DISQUS_ID');
                    disqus_identifier = _this.id;
                    disqus_url = document.URL;         
                    disqus_title = _this.bookObj.get('content').title;

                    (function() {
                        var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
                        dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
                        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
                    })();

                }
            });
        },
    });

    return App.Models.Disqus;
});

【问题讨论】:

    标签: javascript jquery backbone.js requirejs disqus


    【解决方案1】:

    正如DISQUS.reset documentation 所述,您需要重置 Disqus 线程的标识符和 URL。这是通过文档中提供的两个变量完成的:this.page.identifierthis.page.url。现在,您正在 Disqus 中重置页面标题和语言。请尝试重置标识符和 URL。

    【讨论】:

    • 请注意,在带有 Disqus 2012 的 Firefox 上一切正常,在任何带有旧插件的浏览器上也可以正常工作。
    【解决方案2】:

    我也遇到了这个问题,但是上面的解决方案(删除 DISQUS 并在页面上重新加载脚本)在严格模式下不起作用。尝试删除 DISQUS 会导致错误“Delete of an unqualified identifier in strict mode”。

    我通过确保我从未移除 Disqus 自身附加到的容器来解决了这个问题,这是我在用通过 ajax 新加载的帖子交换帖子时所做的。奇怪的是它在没有这个修复的情况下在 Firefox 中工作,但现在它在任何地方都可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-31
      • 2013-02-03
      • 2013-12-23
      • 2012-03-29
      • 1970-01-01
      • 2012-02-23
      • 1970-01-01
      相关资源
      最近更新 更多