views:

519

answers:

4

Hello guys,

i was wondering how can i load the html code of a page, hosted on a different domain?

I am using javascript, and want to create a bookmarklet that will enable me to parse the external html code.

i have been googling for hourse in vain...

thank you very much

A: 

JavaScript isn't allowed to make cross-domain requests. It's a big security risk. Instead, you'll have to execute a script on the server and have it return the results to your JavaScript function.

For example, assuming that you're using JavaScript and PHP you could setup the application to work like this:

JavaScript initiates an Ajax request to a page (or script) located on your server. It passes any required parameters to this page. The following code is based on jQuery (for the sake of being concise), but the principles are the same regardless of your framework.

var sParameters = " ... " // this is defined by you
$.ajax({
  url: 'your-server-side-code.php',
  processData: false,
  data: sParameters,
  success: function(sResponse) {
    // handle the response data however you want
  }
});

The server-side code will respond to the request and pass along the necessary parameters to the cross-domain website. PHP's cURL library is good for this.

// very contrivuted cURL configuration for purposes of example...
$curl_connection = curl_init();
$str_url = "http://you-url.com";
curl_setopt($curl_connection, CURLOPT_URL, $str_url);
curl_setopt($curl_connection, CURLOPT_GET, 1);
// ... keep setting your options ...
$str_response = curl_exec($curl_connection);
curl_close($curl_connection);

When the cross-domain website responds, your server-side code can echo the response back to the initial request. This should probably be validated before responding back, but it's just an example.

print_r($str_response);

A JavaScript response handler function can then parse the incoming response data. Note the success function in the first block of JavaScript code above.

Tom
Note that this is a pain when the page isn't accessible from the server.
MSalters
I'am afraid i don't have a curl enabled server.
A: 

If you don't own the other page, it'll be very difficult.

In fact, you can't make a request to another domain in javascript. The only thing you can do is load a script from another domain:

<script type="text/javascript" src="http://otherdomain.com/script.js"&gt;&lt;/script&gt;

If the purpose of this is to load data from the other domain, and (as I said) you own the other site, you can create the script.js file, which will load the data you need in your original site.

Remember this is a delicate thing, and should be done only if you know what there will be in the script.

FWH
A: 

You can make cross-domain requests from localhost, but if you plan to deploy this code to a server, it's not going to work. Since you are developing a bookmarklet, I think you can do this.

You'll need to use AJAX to get the remote HTML.

The jQuery library makes this task as simple as this...

$.get("http://www.google.com", function(html) { alert(html); });
Josh Stodola
Thanks but it works only if i run the bookmarklet while visiting google.com
A: 

You need to use JSONP and you probably will also need to use a proxy to format the reply to go cross domain. You can do it pretty easily using jQuery and PHP. I have an open source project doing cross domain here: http://os.adamaltemus.com/social-actions/

Corv1nus
any faster way to do that ?
Faster how, as in quicker to develop or quicker to execute?
Corv1nus