使用Javascript查找不同域中的元素属性 [英] Find element attributes on different domain using Javascript
问题描述
是否可以在一个网站上查找某些元素的属性并将其显示在另一个网站上?例如,如果我有网站1,我可以使用Javascript / jQuery来查找网站2上的特定图像或div的大小,并在网站1上显示这些属性吗?
Is it possible to find the attributes of certain elements on one website and display them on another website? For example, if I have website 1, can I use Javascript/jQuery to find out the size of a specific image or div on website 2 and display those attributes on website 1?
如果我不能用Javascript做这样的事情,有没有办法完成这个具体的例子?
If I can't do something like that with Javascript, is there an alternative way of going about accomplishing that specific example?
推荐答案
1 。您尝试做的是不能使用任何AJAX库。浏览器的跨域策略将不允许您这样做。
1. What you are trying to do can't be done using any AJAX library. Browsers' cross-domain policy won't allow you to do this.
但是,您可以使用php(或任何其他服务器端语言)和AJAX。创建一个这样的php脚本:
But you can do this with a combination of php (or any other server-side language) and AJAX. Create a php script like this:
<?php
$url=$_POST['url'];
if($url!="")
echo file_get_contents($url);
?>
让我们说脚本的名称是 fetch.php
。
现在,您可以将AJAX调用从您的jQuery代码转移到此 fetch.php
,并将为您提取HTML代码。
Let us say the script's name is fetch.php
.
Now you can throw an AJAX call from your jQuery code to this fetch.php
and it will fetch the HTML code for you.
2 。相同的来源适用。尝试这个代码,你会面临安全错误,
2. The same origin applies. try this code and you'll face security error,
$.get("other web page site", {}, function(content){
$("#receipe").html(content)
}, "html")
3 。使用Greasemonkey,可以进行第三方请求。在此页面。 Greasemonkey的简短答案是代表您的请求。将所有 XMLHttpRequest
对象替换为 GM_xmlhttpRequest
对象
3. Using Greasemonkey, it is possible to make third-party requests. A jQuery-oriented tutorial is offered on this page. The short answer it to have Greasemonkey make the request on your behalf. Replace all your XMLHttpRequest
objects with GM_xmlhttpRequest
objects.
有用的链接
这篇关于使用Javascript查找不同域中的元素属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!