In JavaScript, is there a way to get source code of a given URL?

前端 未结 3 1685
感情败类
感情败类 2021-01-23 07:54

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

3条回答
  •  生来不讨喜
    2021-01-23 08:26

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

    $.get(
        'yourfile.js', 
        function(data) { 
            // The source code is in data
        } 
    );
    

提交回复
热议问题