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.)
You can also use a iframe to emulate an ajax request. This saves you the mess of having to code a Backend solution for a Frontend problem. Here is an example:
That should do the trick. Please note that the Browser detection line is not the cleanest, you should absolutely use the ones provided in all the most common JS frameworks (Prototype, JQuery, etc)
You can only access cross domain with the following 4 condition
Access-Control-Allow-Origin: *
Demo
Demo:
Allow-Control-Allow-Origin: *
Chrome
Firefox
end
$.get(); can get data from jsbin.com but i don't know why it can't get data from another site like google.com
demo: http://jsfiddle.net/Xj234/ tested with firefox, chrome and safari.