Javascript 一页上有几个主题
我们有一个网站,在那里我们列出了许多活动,并希望在每个活动中添加讨论 所以我们想使用disqs,并检查了它。事实证明,它们使用全局变量来配置实例 喜欢 这给我们带来了一个问题,因为我们不希望使用相同的标识符,而是希望每个实例使用唯一的标识符。尝试将每个实例化+配置放在iFrame中,但这确实搞砸了ie8。有更好的方法吗 总而言之,;一页上有几篇论文。怎样? 是别人干的吗Javascript 一页上有几个主题,javascript,global-variables,disqus,Javascript,Global Variables,Disqus,我们有一个网站,在那里我们列出了许多活动,并希望在每个活动中添加讨论 所以我们想使用disqs,并检查了它。事实证明,它们使用全局变量来配置实例 喜欢 这给我们带来了一个问题,因为我们不希望使用相同的标识符,而是希望每个实例使用唯一的标识符。尝试将每个实例化+配置放在iFrame中,但这确实搞砸了ie8。有更好的方法吗 总而言之,;一页上有几篇论文。怎样? 是别人干的吗 谢谢您可以通过iframe加载每个实例。不过,您可能必须在页面中有滚动条。。。糟糕。我们也遇到了类似的问题,并通过电子邮件对此
谢谢您可以通过iframe加载每个实例。不过,您可能必须在页面中有滚动条。。。糟糕。我们也遇到了类似的问题,并通过电子邮件对此进行了讨论。他们确认,默认情况下,每页只支持一个DISKS模块 在浏览Discus JS文档时,我确实找到了一个解决方案,可以在用户与站点交互时加载和卸载Discus模块:
DISQUS.reset({
reload: true,
config: function () {
this.page.identifier = "newidentifier";
this.page.url = "http://example.com/#!newthread";
}
});
具体的实现将取决于您的站点,但这将为您提供一个构建块。例如,如果事件信息通过扩展内容区域而变得可用,则您可以在有人扩展事件内容时加载Discus模块。我写了一篇关于此的文章,请在此处查找 本质上,如果您可以一次显示一个模块并使用某种“show comments”控件,那么您可以通过以下方式来实现(以Wordpress和jQuery为例,但您可以根据需要调整内容标识符)。在post循环中,为以下各项插入一个额外控件:
<a onclick="loadDisqus(jQuery(this), '<?= $id ?> <?= $post->guid ?>', '<? the_permalink() ?>');">
Show comments
</a>
据推测,从2012年7月17日起,Disqs 2012现在再次支持“重置”。我需要使用GWT应用程序中的Disqs,因此我需要解决在应用程序中的虚拟页面发生更改时按需加载线程的问题
少量的逆向工程和实验让我构建了一个实用程序类(见下文)
主要见解如下:
有一个未记录的全局参数,名为discus\u container\u id
它允许你把评论放在任何你喜欢的地方。如果这在某些情况下不起作用
在未来的版本中,我的备用方案是临时设置目标的id
元素,添加注释,然后将其更改回原始id
因为这是使用JSNI为GWT开发的,所以我需要设置全局
原始窗口上下文中的参数,可通过$wnd
访问。我变了
默认值将相应地取消嵌入代码。我以前没有意识到这一切都是全球性的
变量在窗口对象中,但我学到了一些新的东西
您可以重复使用相同的容器,当您
激活它
这会在DOM中留下大量脚本标记的副本。也许这会是个好主意
一旦它们被使用了,我想把它们也清理干净。或者,我可以做一些
使用其他答案中描述的discus.reset
方法进行更多实验
为单独使用JS的用户提取关键信息,这将允许您将DISKS线程粘贴到任何您喜欢的地方:
function loadComments(container_id, shortname, identifier, developer) {
// CONFIGURATION VARIABLES
window.disqus_container_id = container_id;
window.disqus_developer = developer ? 1 : 0;
window.disqus_shortname = shortname; // required
if (identifier) window.disqus_identifier = identifier;
// DON'T EDIT BELOW THIS LINE
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
}
这是完整的GWT实用程序类。到目前为止,我只实现了所需的参数
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.ui.Widget;
public class Disqus {
public static boolean developer = false;
public static String shortname;
public static void showComments(Widget where, String identifier) {
showComments(where.getElement(), identifier);
}
public static void showComments(Element where, String identifier) {
if (shortname == null)
throw new IllegalArgumentException(
"You must specify the disqus shortname before displaying comments");
// Store the original id of the target element
String id = where.getId();
if (id == null) {
id = "disqus-" + Integer.toHexString(Random.nextInt());
where.setId(id);
}
// Update the id temporarily
where.setId("disqus_thread");
// Load the comments
loadComments(id, shortname, identifier, developer);
}
private static native void loadComments(String container_id, String shortname, String identifier, boolean developer) /*-{
// CONFIGURATION VARIABLES
$wnd.disqus_container_id = container_id;
$wnd.disqus_developer = developer ? 1 : 0;
$wnd.disqus_shortname = shortname; // required
if (identifier) $wnd.disqus_identifier = identifier;
// TODO
// disqus_url
// disqus_title
// disqus_category_id
// DON'T EDIT BELOW THIS LINE (sorry, I've edited it anyway)
(function() {
var dsq = $doc.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + shortname + '.disqus.com/embed.js';
($doc.getElementsByTagName('head')[0] || $doc.getElementsByTagName('body')[0]).appendChild(dsq);
})();
}-*/;
}
我知道这个问题很老了,但因为我也遇到过同样的问题,所以我找到了一个对我来说很好的工作
我目前有一个页面,我们称之为相册,它列出了属于该相册的一系列图像
点击一个图像将弹出一个带有当前图像的灯箱和一个特殊的边栏,该边栏通过ajax获取当前图像信息,如标题、日期、作者、评论等。。(非常类似于facebook图像查看器/侧边栏评论)
我希望用户能够在主相册页面上发表评论,也可以在lightbox侧边栏中查看特定的图像
多亏了一些属于lightbox的回调函数,每次打开lightbox时都会运行一个回调函数,我用它暂时将主相册页面中的div“discus_thread”重命名为其他函数
每当您更改lightbox中的图像时,就会运行另一个回调,这允许我重新加载有关图像的侧栏信息,其中包括一个新的discus_线程div和一个强制discus_重置的javascript
另一个回调在lightbox关闭时运行,这允许我将相册注释div重命名回discus_线程并强制重新设置
总而言之,主页包含相册的注释,当你点击一张图片时,我会将原始div重命名为其他内容。然后通过AJAX获取一些信息,其中包含一个新的discus_线程div。我使用discus.reset并在lightbox上加载注释。当我关闭lightbox时,我将原始div重命名回discus_线程,并强制重新设置
我希望它能帮助别人 Tente isso:
<div class="showDisqus" data-title="MyTitle" data-id="1" data-url="mysite.com/mypost">Show Comments</div>
$('.showDisqus').on('click', function(){ // click event of the show comments button
var this_ = $(this);
disqus_shortname = 'your_shortname',
title = $(this).attr('data-title'),
identifier = parseFloat($(this).attr('data-id')),
url = $(this).attr('data-url');
if (window.DISQUS) {
DISQUS.reset({ // Remove the old call
reload: false,
config: function () {
this.page.identifier = window.old_identifier;
this.page.url = window.old_url;
this.page.title = window.old_title;
}
});
$('.showDisqus').show();
$('#disqus_thread').remove();
$('<div id="disqus_thread"></div>').insertAfter(this_);
setTimeout( function() { // Creates a new call DISQUS, with the new ID
DISQUS.reset({
reload: true,
config: function () {
this.page.identifier = identifier;
this.page.url = url;
this.page.title = title;
}
});
window.old_identifier = identifier;
window.old_url = url;
window.old_title = title;
});
} else {
var disqus_identifier = parseFloat(identifier),
disqus_title = title,
disqus_url = url;
$('<div id="disqus_thread"></div>').insertAfter(this_);
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
setTimeout( function() { // Sorry, there must be a better way to force the ID called correctly
DISQUS.reset({
reload: true,
config: function () {
this.page.identifier = identifier;
this.page.url = url;
this.page.title = title;
}
});
},500);
window.old_identifier = identifier;
window.old_url = url;
window.old_title = title;
}
$(this).fadeOut(); // remove the show comments button
});
显示注释
$('.showDiscus')。在('click',function(){//单击显示注释按钮的事件
var this=$(this);
discus_shortname='您的_shortname',
title=$(this.attr('data-title'),
identifier=parseFloat($(this).attr('data-id')),
url=$(this.attr('data-url');
if(窗口取消){
disqs.reset({//删除旧调用
重新加载:false,
配置:函数(){
this.page.identifier=window.old_标识符;
this.page.url=window.old_url;
this.page.title=window.old_title;
}
});
$('.showDiscus').show();
$(“#取消线程”).remove();
$('').insertAfter(本);
setTimeout(function(){//使用新ID创建一个新的调用discus
重设({
是的,
配置:函数(){
这
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.ui.Widget;
public class Disqus {
public static boolean developer = false;
public static String shortname;
public static void showComments(Widget where, String identifier) {
showComments(where.getElement(), identifier);
}
public static void showComments(Element where, String identifier) {
if (shortname == null)
throw new IllegalArgumentException(
"You must specify the disqus shortname before displaying comments");
// Store the original id of the target element
String id = where.getId();
if (id == null) {
id = "disqus-" + Integer.toHexString(Random.nextInt());
where.setId(id);
}
// Update the id temporarily
where.setId("disqus_thread");
// Load the comments
loadComments(id, shortname, identifier, developer);
}
private static native void loadComments(String container_id, String shortname, String identifier, boolean developer) /*-{
// CONFIGURATION VARIABLES
$wnd.disqus_container_id = container_id;
$wnd.disqus_developer = developer ? 1 : 0;
$wnd.disqus_shortname = shortname; // required
if (identifier) $wnd.disqus_identifier = identifier;
// TODO
// disqus_url
// disqus_title
// disqus_category_id
// DON'T EDIT BELOW THIS LINE (sorry, I've edited it anyway)
(function() {
var dsq = $doc.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + shortname + '.disqus.com/embed.js';
($doc.getElementsByTagName('head')[0] || $doc.getElementsByTagName('body')[0]).appendChild(dsq);
})();
}-*/;
}
<div class="showDisqus" data-title="MyTitle" data-id="1" data-url="mysite.com/mypost">Show Comments</div>
$('.showDisqus').on('click', function(){ // click event of the show comments button
var this_ = $(this);
disqus_shortname = 'your_shortname',
title = $(this).attr('data-title'),
identifier = parseFloat($(this).attr('data-id')),
url = $(this).attr('data-url');
if (window.DISQUS) {
DISQUS.reset({ // Remove the old call
reload: false,
config: function () {
this.page.identifier = window.old_identifier;
this.page.url = window.old_url;
this.page.title = window.old_title;
}
});
$('.showDisqus').show();
$('#disqus_thread').remove();
$('<div id="disqus_thread"></div>').insertAfter(this_);
setTimeout( function() { // Creates a new call DISQUS, with the new ID
DISQUS.reset({
reload: true,
config: function () {
this.page.identifier = identifier;
this.page.url = url;
this.page.title = title;
}
});
window.old_identifier = identifier;
window.old_url = url;
window.old_title = title;
});
} else {
var disqus_identifier = parseFloat(identifier),
disqus_title = title,
disqus_url = url;
$('<div id="disqus_thread"></div>').insertAfter(this_);
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
setTimeout( function() { // Sorry, there must be a better way to force the ID called correctly
DISQUS.reset({
reload: true,
config: function () {
this.page.identifier = identifier;
this.page.url = url;
this.page.title = title;
}
});
},500);
window.old_identifier = identifier;
window.old_url = url;
window.old_title = title;
}
$(this).fadeOut(); // remove the show comments button
});
<script type="text/javascript">
var disqus_shortname = 'superchocolate',
disqus_identifier = 'default',
disqus_title = 'I Heart Chocoloate',
disqus_config = function(){
this.language = 'en';
};
(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);
})();
function loadDisqus( identifier, url, title )
{
DISQUS.reset({
reload: true,
config: function ()
{
this.page.identifier = identifier;
this.page.url = url;
this.page.title = title;
this.language = 'en';
}
});
}
</script>
<div id="disqus_thread"></div>
loadDisqus( 'ugc-' + data.id, location.protocol+'//'+location.hostname + "/ugc-submission-" + data.id + "/", data.title );