I’d like to know how to use XMLHttpRequest to load the content of a remote URL and have the HTML of the accessed site stored in a JS variable.
Say, if I wanted to load and alert() the HTML of http://foo.com/bar.php, how would I do that?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can get it by
XMLHttpRequest.responseTextinXMLHttpRequest.onreadystatechangewhenXMLHttpRequest.readyStateequals toXMLHttpRequest.DONE.Here’s an example (not compatible with IE6/7).
For better crossbrowser compatibility, not only with IE6/7, but also to cover some browser-specific memory leaks or bugs, and also for less verbosity with firing ajaxical requests, you could use jQuery.
Note that you’ve to take the Same origin policy for JavaScript into account when not running at localhost. You may want to consider to create a proxy script at your domain.