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?

If I can't do something like that with Javascript, is there an alternative way of going about accomplishing that specific example?

user715564
  • 1,650
  • 2
  • 25
  • 60
  • 1
    Generally, code on a page from one domain can't access anything fetched from a different domain. If you know the URL for an image (which you cannot get from the HTML fetched from another domain, remember), you can fetch the image and get its size. – Pointy Aug 18 '13 at 17:38
  • 3
    Unless you have control over both websites, this is not possible due to javascripts same origin policy. It is however trivial to do in just about any serverside language, like PHP etc. If it's an image, you can just load it by referencing it and check the size with javascript. – adeneo Aug 18 '13 at 17:38

2 Answers2

1

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.

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);
?>

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. 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. 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.

Useful links,

Can Javascript read the source of any web page?

http://www.sitepoint.com/forums/showthread.php?836704-How-to-get-contents-of-3rd-party-website-into-javascript-variable

Community
  • 1
  • 1
Optimus Prime
  • 6,817
  • 5
  • 32
  • 60
  • Ok, I see. It seems like the best way is to use a separate PHP script to load the other websites contents into. Do you know what the performance is for this method? – user715564 Aug 18 '13 at 17:44
  • Not sure, but I see no reason to worry about performance. You need to just use ajax to instantiate and get the response from the php file. So the performance should be what performance of an ajax call is. – Optimus Prime Aug 18 '13 at 17:47
  • Ok, perfect. The first link you gave me, using YQL, actually looks like it might be a great option as well. Anyway, thanks for the help. – user715564 Aug 18 '13 at 17:54
0

Sadly because of same origin policy you can't access the DOM on a different domain. If you control both domains you maybe able to use CORS and modify the server HTTP headers to allow Javascript access.

The workaround to this is to use a server to act as a proxy between the two websites. So you would have a server side script on website 1 that would send a request to website 2 and return the content from website 1.

Steven V
  • 16,357
  • 3
  • 63
  • 76
  • If you control both domains, you can also use [window.postMessage](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage) to send the `innerHTML` of a DOM element to another domain. – Anderson Green Apr 11 '22 at 19:23