JavaScript 可以用来加载另一个页面的元数据内容吗?
在我的网站上,所有页面的 中都有一个作者:
<meta name="Author" content="Mark Brown" />
我想创建一个链接页面,并在每个链接旁边使用 jQuery 调用该页面的作者元数据内容值。
我尝试了这个,但没有成功:
.load( '{URLHERE}' + $('meta[name=Author]').attr('content') )
抱歉,我是 JS 新手。我似乎 .load()
只能获取 body
标记内的内容?是这样吗?我有什么想法怎么做吗?
On my website all the pages have an author in the <head>
:
<meta name="Author" content="Mark Brown" />
I want to create a page of links and next to each call that page's Author metadata content value with jQuery.
I tried this and it didn't work:
.load( '{URLHERE}' + $('meta[name=Author]').attr('content') )
Sorry I'm a JS Newbie. I seems that .load()
only gets content inside the body
tag? Is that right? Any ideas how I'd do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
.load
将内容加载到目标元素中,这似乎是您不想要的。您想使用.get
编辑:在指出此代码从当前页面而不是所需的页面获取数据后,我尝试了这样的操作:
但事实证明,即使结果有元标记,查找不会返回任何内容。尝试查找 div
$(result).find('div')
效果很好。一种可能性是将结果附加到 dom 元素并查询:它可以工作,但速度非常慢。我建议使用服务器端方法来检索这些数据。如果您有 X 个链接,客户端解决方案将发出 X 个请求,这可能是一种浪费。
.load
loads the content into the destination element, which you seem not to want. You want to use.get
Edit: after being pointed out that this code fetches data from the current page and not the desired one, I tried something like this:
but it turns out that, even though result has a meta tag, the find doesn't return anything. Trying to find a div
$(result).find('div')
works fine though. A possibility is appending the result to a dom element and querying:It works but it's deadly slow. I would recommend a server side approach to retrieve these data. A client side solution would make X requests if you have X links, that's probably a waste.
您可以分两个阶段使用
load
事件:这将首先将
meta
标记加载到您的目标中,然后使用complete
回调,可以获取Author属性的值。You can use the
load
event in two stages:This will first load the
meta
tag into your destination and by using thecomplete
callback
, you can get the value of the Author attribute.你是对的,来自文档:
您可以创建一个小型服务器端脚本,用于获取文档并为您进行 html 解析并相应地返回元数据。然后您可以通过 ajax 调用该脚本。
you are right, from the docs:
You could create a small serverside script which fetches the document and does the html parsing for you and returns the metadata accordingly. You can then call this script via ajax.