Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

清理评论加载的代码 #586

Merged
merged 5 commits into from
Jul 14, 2017
Merged

清理评论加载的代码 #586

merged 5 commits into from
Jul 14, 2017

Conversation

yixuan
Copy link
Member

@yixuan yixuan commented Jul 14, 2017

根据 #577 中的建议清理了下代码。

@yixuan yixuan requested a review from yihui July 14, 2017 00:00
}
}
}
comments_delayed_loading("comment-box", "forum-frame", "{{ .Site.Params.comment_embed_url }}{{ .Params.forum_id }}");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个做法仍然暴露了一个全局对象 comments_delayed_loading。我的建议是把 iframe 真正的 src 属性临时存储在 data-src 属性中,然后你的 comments.js 脚本就可以实现零参数运行了。把 data-src 属性读出来赋给 src 属性,iframe 就加载了。

我之所以想让你用这个办法,是因为这个办法可以很容易移植给 #579 用(只不过应用给 <img> 对象而已),一箭双雕。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

明白。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

但 ID 是不是就得写死进函数里了?

Copy link
Member

@yihui yihui Jul 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不,ID 写在外面:<iframe id="forum-frame" data-src="{{ .Site.Params.comment_embed_url }}{{ .Params.forum_id }}"></iframe>,函数里面只做一件事,就是把 data-src 替换为 src。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

哦,我是说要找到这个 <iframe> 就得知道它的 ID forum-frame,这个是不是得写进匿名函数里?还是说默认对所有 <iframe> 都这么来一遍?但即使是这样也得知道外面那个盒子的 ID。

Copy link
Member

@yihui yihui Jul 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

处理所有带 data-src 属性的 iframe:document.getElementsByTagName('iframe')

function show_comment_box() {
var frame = document.getElementById(iframe_id);
frame.src = iframe_url;
frame.style.height = "800px";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

800有没有略大?这个我也没有什么意见。

}
}
</script>
<script type="text/javascript" src="/js/comments.js"></script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

理论上此处应该用 {{ relURL "/js/comments.js" }} 以保证可移植性,不过在这个网站里问题不大。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我是从 search.js 那儿复制过来的。:joy:
统一改一下吧。

@yihui yihui merged commit cb303c3 into cosname:master Jul 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants