0

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

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

This answer is incomplete. It doesn't explain for which URLs this works.
-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.

3 Comments

Doesn't work: jsfiddle.net/m28Qx (FYI, you can't make Ajax calls to external URLs).
Yes, because some pages restrict their content-policy to same-origin.
Exactly, but you didn't mention that in your answer originally.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.