Brandalf
Brandalf

Reputation: 517

Use Javascript to get the source code of another website using a URL

How can I use JavaScript to view the html source code of a website using a URL

Upvotes: 0

Views: 2532

Answers (1)

Vivek Doshi
Vivek Doshi

Reputation: 58613

Here is the code that you are looking for :

$.ajax({
   url: 'http://www.somesite.com/',
   type: 'GET',
   success: function(res) {
      var data = $.parseHTML(res);  //<----try with $.parseHTML().
      // data you will get full html code from given url

      // this is how you can fetch any element from html that fetched from url
      $(data).find('div.content').each(function(){
          $('#here').append($(this).html());
     });

   }
 });

Note : This is assuming that the website enables CORS which is pretty uncommon for most sites in general.( as Patrick Roberts said. Thanks for this point :) )

Upvotes: 2

Related Questions