-
Notifications
You must be signed in to change notification settings - Fork 243
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
清理评论加载的代码 #586
Conversation
layouts/partials/comments.html
Outdated
} | ||
} | ||
} | ||
comments_delayed_loading("comment-box", "forum-frame", "{{ .Site.Params.comment_embed_url }}{{ .Params.forum_id }}"); |
There was a problem hiding this comment.
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>
对象而已),一箭双雕。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
明白。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
但 ID 是不是就得写死进函数里了?
There was a problem hiding this comment.
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。
There was a problem hiding this comment.
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。
There was a problem hiding this comment.
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')
static/js/comments.js
Outdated
function show_comment_box() { | ||
var frame = document.getElementById(iframe_id); | ||
frame.src = iframe_url; | ||
frame.style.height = "800px"; |
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
理论上此处应该用 {{ relURL "/js/comments.js" }}
以保证可移植性,不过在这个网站里问题不大。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我是从 search.js
那儿复制过来的。:joy:
统一改一下吧。
根据 #577 中的建议清理了下代码。