0

how can I retrieve full web page source from URL into string variable(object) in JavaScript?

Timigen
  • 1,055
  • 1
  • 17
  • 33

3 Answers3

1

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

Community
  • 1
  • 1
JensB
  • 6,663
  • 2
  • 55
  • 94
0

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

thispatchofsky
  • 854
  • 6
  • 15
-1

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.

Community
  • 1
  • 1
Hannes Schneidermayer
  • 4,729
  • 2
  • 28
  • 32