Thursday, April 25, 2024
52
rated 0 times [  59] [ 7]  / answers: 1 / hits: 70597  / 16 Years ago, mon, february 23, 2009, 12:00:00

How can I make requests to other server(s) (i.e. get a page from any desired server) with a JavaScript within the user's browser? There are limitations in place to prevent this for methods like XMLHttpRequest, are there ways to bypass them or other methods?



That is a general question, specifically I want to check a series of random websites and see if they contain a certain element, so I need the HTML content of a website without downloading any additional files; all that in a JavaScript file, without any forwarding or proxy mechanism on a server.



(Note: one way is using Greasemonkey and its GM_xmlhttpRequest.)


More From » cross-domain

 Answers
21

You should check out jQuery. It has a rich base of AJAX functionality that can give you the power to do all of this. You can load in an external page, and parse it's HTML content with intuitive CSS-like selectors.



An example using $.get();



$.get(anotherPage.html, {}, function(results){
alert(results); // will show the HTML from anotherPage.html
alert($(results).find(div.scores).html()); // show scores div in results
});


For external domains I've had to author a local PHP script that will act as a middle-man. jQuery will call the local PHP script passing in another server's URL as an argument, the local PHP script will gather the data, and jQuery will read the data from the local PHP script.



$.get(middleman.php, {site:http://www.google.com}, function(results){
alert(results); // middleman gives Google's HTML to jQuery
});


Giving middleman.php something along the lines of



<?php

// Do not use as-is, this is only an example.
// $_GET[site] set by jQuery as http://www.google.com
print file_get_contents($_GET[site]);

?>

[#99941] Tuesday, February 17, 2009, 16 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ammonderekm

Total Points: 247
Total Questions: 105
Total Answers: 98

Location: Tuvalu
Member since Sat, Feb 11, 2023
1 Year ago
ammonderekm questions
Tue, May 24, 22, 00:00, 2 Years ago
Wed, Mar 11, 20, 00:00, 4 Years ago
Thu, Dec 19, 19, 00:00, 4 Years ago
Thu, Oct 24, 19, 00:00, 5 Years ago
;