PDA

View Full Version : Get specified page source as string



Fighterfox
01-20-2011, 03:00 PM
I would like to get the source of any page I specify as a JavaScript string. I can use

window.location = "view-source:http://www.dynamicdrive.com";

to print the source code to the user, but that's not what I want to do.

Can I get the entire source of a page (e.g. http://www.dynamicdrive.com) as a JavaScript string, so that I can analyze and extract the parts which I'm interested in.

traq
01-20-2011, 03:38 PM
I'd image you could use something like this:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
$.ajax({
url: "view-source:http://www.custom-anything.com"
,dataType: "text"
,success: function(data){
$("#div").text(data);
}
,error: function(xhr,erstr){ alert(erstr); }
});
});
</script>
, though it doesn't work in present form (I get an error about a non-functional http protocol handler). I'll keep tinkering with it, though there are others here who know more about javascript.



This can be done from php (and other server-side languages), but with js, you may be stuck behind the same-domain rule.
(although, at present, I can't get it to work at all.)