how can I retrieve full web page source from URL into string variable(object) in JavaScript?
3 Answers
Javascript has a same origin policy which makes it so you can only retrieve information from your own domain. This is to prevent you from creating malicious scripts that access websites the client is unaware of.
If all you want to do is retireve information from the same domain as your page is hosted on check out jquery / get https://api.jquery.com/jQuery.get/
$.get( "ajax/test.html", function( data ) {
$( ".result" ).html( data );
alert( "Load was performed." );
});
If you must access external information I suggest you proxy the call to the external page through your server. Ie you make a call to a page on your server which in turn requests the external page and returns this to your script.
There are ways to circumvent the same origin policy, you can read more about those here Ways to circumvent the same-origin policy.
I woulden't use these methods to create a public website as most of the methods are probably hacks or bugs that will be fixed eventually.
If you control the external server, or they accept JSONP requests this could also be an option http://en.wikipedia.org/wiki/JSONP
Is this what you are looking for:
$.get(url,function(data){var yourObject = data});
Edit based on comment:
If you are looking for the page source of the current page, replace url with window.location.pathname
. Note that you will not be able to make the server being pinged is CORS enabled

- 854
- 6
- 15
-
This answer is incomplete. It doesn't explain for which URLs this works. – Felix Kling May 08 '14 at 14:35
Here is how to do it (won't work if a page has restricted their content-policy to same-origin):
var page;
$.get('http://stackoverflow.com',function(data){ page= data });
You now have saved the html in the variable page
.
Another way is using this answer.
var req = new XMLHttpRequest();
req.open('GET', 'http://www.mydomain.com/', false);
req.send(null);
if(req.status == 200)
dump(req.responseText);
To get around the same-origin issue, please consider this answer.

- 1
- 1

- 4,729
- 2
- 28
- 32
-
Doesn't work: http://jsfiddle.net/m28Qx/ (FYI, you can't make Ajax calls to external URLs). – Felix Kling May 08 '14 at 14:34
-
Yes, because some pages restrict their content-policy to **same-origin**. – Hannes Schneidermayer May 09 '14 at 07:06
-