通过id进行跨域请求
我正在尝试在我的网页上显示来自其他网站的图像拼贴画。如果我知道直接链接,它就可以正常工作,因为我可以设置 img src。但是,如果我只知道页面的名称和该页面上图像的 id,就可以进行一些 jQuery 调用来检索 src。
像这样的东西,但在另一个页面上调用:
var imageSource = $("#pictureid").attr("src");
I'm trying to display a collage of images from other sites on my webpage. It works fine if I know the direct link because I can set the img src. However if I only know the name of the page and the id of the image on that page can make some jQuery call to retrieve the src.
Something like this, but called on another page:
var imageSource = $("#pictureid").attr("src");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您想要的内容是对其他网站进行 AJAX 调用以提取 HTML,然后查看它以找到您要使用的
img
标记的src
属性所有现代浏览器都有某种同源策略,但禁止这样做 - 您不能对当前文档域之外的任何内容进行 AJAX 调用(也就是说,在 example.org 的页面中运行的脚本不能进行 AJAX 调用到foobar.com)。这意味着您必须求助于服务器端技术:在您选择的服务器端语言中,从外部页面加载 HTML(大多数环境都绑定到 libcurl,它的设计正是为了执行此操作),然后解析它找到图像。获得 URL 后,您可以轻松地将其插入到动态 HTML 中。
也就是说,未经许可深度链接图像有点违反网络礼仪,甚至可能是非法的,具体取决于您所在的司法管辖区(您正在滥用其他人的服务器为访问者提供带宽,毕竟,这可能会在某些司法管辖区属于非法黑客的定义);最好的做法是也将图像本身拉入并在您自己的服务器上托管一个副本,以便您可以使用自己的带宽来为其提供服务。当然,请尊重您使用的图像的版权和许可条款(如果有)。
What you want would involve making an AJAX call to that other site to pull in the HTML, and then look through that to find the
src
attribute of theimg
tag you're interested in. All modern browsers have some sort of Same Origin Policy in place though which forbids this - you cannot make AJAX calls to anything outside the current document's domain (that is, a script running in a page on example.org cannot make AJAX calls to foobar.com).This means that you have to resort to server-side techniques: In the server-side language of your choice, load the HTML from the external page (most environments have bindings into libcurl, which was designed to do exactly this) and then parse that to find the image. Once you have the URL, you can easily insert it into your dynamic HTML.
That said, deep-linking images without permission kind of violates netiquette, and it may even be illegal, depending on the jurisdiction you're in (you're abusing someone else's server to provide bandwidth to your visitors, after all, something that might fall under the definition of illegal hacking in some jurisdictions); the nice thing to do is to pull the image itself in as well and host a copy on your own server, so that you are using your own bandwidth to serve it. And of course, respect the copyright and, if any, licensing terms, of the images you use.
客户端 JavaScript 不是为此设计的。你想做的就是屏幕抓取。您可以通过多种方式来完成此操作,最简单的是 Yahoo 的 YQL 以及使用 Node.JS、PHP 或您首选的服务器端语言的其他方式。
Client side javascript isnt designed for this. What you want to do it screen scraping. You can do this in a number of ways, the easiest being Yahoo's YQL and other ways using Node.JS, PHP or your preferred serverside language.
不可以,如果您希望在网站上包含图像,则需要提供该图像的 URL。
No, if you wish to include an image on your site you need to provide the URL for it.
尝试这样的操作:
首先,我们需要一个简短的服务器端脚本来反弹我们的请求,因为 AJAX 受到同源策略的阻碍。这是 PHP 中的一些内容:
假设它保存在
get.php
中。要使用它,我们只需将查询字符串的 url 字段设置为我们想要获取的页面的 url 即可。该脚本从该页面获取所有 HTML 并将其输出。例如,get.php?url=http://stackoverflow.com/
将获取 stackoverflow 索引页中的所有 HTML。接下来,让我们编写一些 jQuery 来检索 url 处指定图片的 src。像这样:
这应该可行。
Try something like this:
First we need a short server-side script to bounce our request, since AJAX is hindered by the same origin policy. Here's something in PHP:
Let's say that this is saved at
get.php
. To use it, we just set the url field of the query string with the url of the page we want to get. The script gets all of the HTML from that page and outputs it. For instance,get.php?url=http://stackoverflow.com/
would get all of the HTML in stackoverfllow's index page.Next, let's write some jQuery to retrieve the src of the specified picture at the url. Like this:
That should work.