JavaScript 可以用来加载另一个页面的元数据内容吗?

发布于 2024-12-28 09:16:26 字数 373 浏览 2 评论 0原文

在我的网站上,所有页面的 中都有一个作者:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〆凄凉。 2025-01-04 09:16:27

.load 将内容加载到目标元素中,这似乎是您不想要的。您想使用 .get

$.get(url, function() {
    var author = $('meta[name=Author]').attr('content');
    // do what you need with author
})

编辑:在指出此代码从当前页面而不是所需的页面获取数据后,我尝试了这样的操作:

$.get(url, function(result) {
    var author = $(result).find('meta[name=Author]').attr('content');
});

但事实证明,即使结果有元标记,查找不会返回任何内容。尝试查找 div $(result).find('div') 效果很好。一种可能性是将结果附加到 dom 元素并查询:

$.get(url, function(result) {
    var dom = $('<div></div>').html(result);
    var author = dom.find('meta[name=Author]').attr('content');
});

它可以工作,但速度非常慢。我建议使用服务器端方法来检索这些数据。如果您有 X 个链接,客户端解决方案将发出 X 个请求,这可能是一种浪费。

.load loads the content into the destination element, which you seem not to want. You want to use .get

$.get(url, function() {
    var author = $('meta[name=Author]').attr('content');
    // do what you need with author
})

Edit: after being pointed out that this code fetches data from the current page and not the desired one, I tried something like this:

$.get(url, function(result) {
    var author = $(result).find('meta[name=Author]').attr('content');
});

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:

$.get(url, function(result) {
    var dom = $('<div></div>').html(result);
    var author = dom.find('meta[name=Author]').attr('content');
});

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.

凉栀 2025-01-04 09:16:27

您可以分两个阶段使用 load 事件:

$("#your-target").load("{URLHERE} meta[name='Author']", function(response, status, xhr){
    var $meta = $("meta", this);

    $(this).text($meta.attr("content"));

    $meta.remove();
});

这将首先将 meta 标记加载到您的目标中,然后使用 complete 回调,可以获取Author属性的值。

You can use the load event in two stages:

$("#your-target").load("{URLHERE} meta[name='Author']", function(response, status, xhr){
    var $meta = $("meta", this);

    $(this).text($meta.attr("content"));

    $meta.remove();
});

This will first load the meta tag into your destination and by using the complete callback, you can get the value of the Author attribute.

残龙傲雪 2025-01-04 09:16:27

你是对的,来自文档:

jQuery 使用浏览器的 .innerHTML 属性来解析检索到的内容
文档并将其插入到当前文档中。在此过程中,
浏览器通常会过滤文档中的元素,例如 html、
标题或头部元素。结果,检索到的元素
.load() 可能与检索文档时不完全相同
直接通过浏览器。

您可以创建一个小型服务器端脚本,用于获取文档并为您进行 html 解析并相应地返回元数据。然后您可以通过 ajax 调用该脚本。

you are right, from the docs:

jQuery uses the browser's .innerHTML property to parse the retrieved
document and insert it into the current document. During this process,
browsers often filter elements from the document such as html,
title, or head elements. As a result, the elements retrieved by
.load() may not be exactly the same as if the document were retrieved
directly by the browser.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文