-1

Using only javascript only how do I hit a url eg www.google.com and get the google logo display on my page.

I tried this using jQuery, but i want in pure javascript.

$.ajax({
   url: 'http://www.somesite.com/',
   type: 'GET',
   success: function(res) {
      $(res).find('div.content').each(function(){
          $('#here').append($(this).html());
     });

   }
 });
Dinǝsh Gupta
  • 377
  • 1
  • 2
  • 9

1 Answers1

1

You're almost there. After you get the data from the server (How to make an AJAX call without jQuery?) you need to parse it and get that page's DOM. Once you have that it's as simple as getting the element that contains the image you want, grabbing the src attribute, and then getting that file. Of course this all depends on the page's structure and form -- a badly made page might be difficult to deal with. Your best bet is to go to the site and either grab the image to store on your system or the link to it.

Here's a question that can help you with parsing the html string into a DOM object: Converting HTML string into DOM elements?

Community
  • 1
  • 1
Richard Barker
  • 1,161
  • 2
  • 12
  • 30
  • Both links in my answer use pure js code. Those two links will give you part of what you need, but in order to get the image directly from the web page you either need to deal with it as a string (which is a pain) or turn it into a DOM object in js in order to treat it as you would treat the web page you're on in js. In either case you need to get the src attribute from the image you want and then use that as the src attribute for the image on your page. – Richard Barker Dec 18 '15 at 21:32
  • can you share a fiddle example ? – Dinǝsh Gupta Dec 18 '15 at 21:57