In JavaScript, is there a way to get source code of a page given its URL? Kind of equivalent to PHP's file_get_contents()
-
Is the HTML you're trying to get hosted on the same domain as your page?user113716– user1137162011-01-08 01:49:09 +00:00Commented Jan 8, 2011 at 1:49
-
2possible duplicate of Can Javascript read the source of any web page?Paul Roub– Paul Roub2015-08-31 02:02:08 +00:00Commented Aug 31, 2015 at 2:02
Add a comment
|
3 Answers
You can just download from a URL through XMLHttpRequest (or jQuery's ajax):
$.get(
'yourfile.js',
function(data) {
// The source code is in data
}
);
2 Comments
Hemlock
One catch is that in Firefox, the script will NOT come from the cache. Super fast for the other browsers though. stackoverflow.com/questions/4467010/…. I still haven't found a good work around for that.
goat
@hemlcok, FF caches scripts via ajax when http headers say it should.
I can do this with Pretty Diff tool api: http://prettydiff.com/api.php
The actual application, prettydiff.js, is JavaScript, and the service processing the requests on the server is also JavaScript. To see this in action go to the test link and use the "Source URI" method of input.