0

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

1
  • 1
    If you have a CORS proxy, it's pretty easy. Otherwise it's not possible. Commented Jun 22, 2017 at 4:14

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

This is assuming that the website enables CORS which is pretty uncommon for most sites in general.
Yes thanks for that , let me add this as note @PatrickRoberts

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.