1

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()

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Babiker
  • 18,300
  • 28
  • 78
  • 125
  • Is the HTML you're trying to get hosted on the same domain as your page? – user113716 Jan 08 '11 at 01:49
  • 2
    possible duplicate of [Can Javascript read the source of any web page?](http://stackoverflow.com/questions/680562/can-javascript-read-the-source-of-any-web-page) – Paul Roub Aug 31 '15 at 02:02

3 Answers3

5

You can just download from a URL through XMLHttpRequest (or jQuery's ajax):

$.get(
    'yourfile.js', 
    function(data) { 
        // The source code is in data
    } 
);
Jacob
  • 77,566
  • 24
  • 149
  • 228
  • One catch is that in Firefox, the script will NOT come from the cache. Super fast for the other browsers though. http://stackoverflow.com/questions/4467010/read-the-source-of-a-script-tag-from-the-cache-in-firefox. I still haven't found a good work around for that. – Hemlock Jan 08 '11 at 01:50
  • @hemlcok, FF caches scripts via ajax when http headers say it should. – goat Jan 08 '11 at 02:47
0

How about this: http://phpjs.org/functions/file_get_contents:400 ?

Chris
  • 648
  • 3
  • 9
  • 20
0

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.

austincheney
  • 807
  • 10
  • 9