Javascript - 获取元关键字和如果非不破坏书签
我有一个书签,可以获取页面上的元关键字。但是,如果没有元关键字,小书签就会中断。
这是我当前的 javascript
function docoument_keywords(){
var keywords;
var metas = document.getElementsByTagName('meta');
for (var x=0,y=metas.length; x<y; x++) {
if (metas[x].name.toLowerCase() == "keywords") {
keywords = metas[x];
}
}
return keywords.content;
}
k = document_keywords();
$('body').append("<p>" + k + "</p><p>Content</p>");
当元关键字中实际有关键字时,书签可以正常工作。然而,当没有的时候它就中断了。你们有什么解决办法吗?
非常感谢!
I have a bookmarklet that gets the meta keywords on a page. However if the there are no meta keywords the bookmarklet breaks.
Here is my current javascript
function docoument_keywords(){
var keywords;
var metas = document.getElementsByTagName('meta');
for (var x=0,y=metas.length; x<y; x++) {
if (metas[x].name.toLowerCase() == "keywords") {
keywords = metas[x];
}
}
return keywords.content;
}
k = document_keywords();
$('body').append("<p>" + k + "</p><p>Content</p>");
The bookmarklet works fine when there are actually keywords in the meta keywords. However its break when there are none. You guys have any solutions?
Much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
工作示例: JS Fiddle
Working example: JS Fiddle
虽然 Michael 回答了你的问题,但我只是想指出,由于你似乎已经在使用 jQuery,所以这可以更简单地完成。您的脚本可以完整地总结如下:
希望它有帮助!
Though Michael answered your question, I just wanted to point out that since you appear to already be using jQuery, this can be accomplished much, much more simply. Your script can be summarized thusly in its entirety:
Hope it's helpful!