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

2

3 Answers 3

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
    } 
);
Sign up to request clarification or add additional context in comments.

2 Comments

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.
@hemlcok, FF caches scripts via ajax when http headers say it should.
0

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

Comments

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.

Comments

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.