有没有人让 Disqus 能够与 PJAX 甚至常规 #!
AJAX 等工具正常工作。
让我们看一些这样做的人的例子:
第一个使用 Hash bang 风格,第二个使用 PJAX。现在单击这些网站,您会注意到在 Disqus 中您的头像图标消失了,如果您打开 JavaScript 控制台,您通常会收到错误和日志消息,因为 DISQUS
对象无法解除绑定(bind)。
我尝试了自己的随机黑客(这很困难,因为我找不到未通过缩小进行混淆的 DISQUS js):
(function() {
if ( document.getElementById('my_disqus_script') && document.getElementById('disqus_thread') ) {
//var ds = document.getElementById('my_disqus_script');
//(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).removeChild(ds);
alert(window.location.href);
DISQUS.reset({
reload: true,
config: function() {
this.page.identifier = 'disqus_thread';
this.page.url = window.location.href + '#!disqus_thread';
}
});
}
else if (document.getElementById('disqus_thread')){
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.id = 'my_disqus_script';
dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
}
else {
var ds = document.getElementById('my_disqus_script');
if (ds)
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).removeChild(ds);
if (window['DISQUS']) {
alert('cleanup');
DISQUS.reset();
DISQUS.cleanup();
DISQUS = null;
}
}
})();
基本上,我尝试了一系列清理技术,以便当我的 PJAX 加载新页面时它应该清理,但我总是收到一些绑定(bind)到 null
的事件,引发异常。
最佳答案
@JanHančič 是正确的,因为 iframe 是最佳路线(所以如果他回答我会标记他正确)。 iframe 的高度和无缝可以通过以下方式处理: https://github.com/house9/jquery-iframe-auto-height
我所做的是创建一个 Controller (或者你的框架所称的任何名称)来处理任何网址,例如:http://my.com/page/disqus
,它将加载的注释http://my.com/page
。
Controller 仅加载 DISQUS 小部件(包装在 html/body
中)。然后在 http://my.com/page
模板中,将 iframe 包含到 http://my.com/page/disqus
。
关于javascript - DISQUS 和 Pushstate AJAX 或 Hashbang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13772178/