How to get a div of another html page with pure js?

Asked

Viewed 254 times

1

I want to upload a list of a div, from one page to another (both on my server). I know that for this there is the function $('#content').load('paginaexemplo.htm' '#lista'), but I’d like to know how I do it with pure.

1 answer

-1


You can do it this way with Xmlhttprequest:

var http = new XMLHttpRequest();

var url_ = "pagina.php"; // página de onde virá a div
http.open("GET", url_, true);
http.onreadystatechange = function(){
   if(http.readyState == 4){
      var html = http.responseText;
      // converte o retorno em elementos HTML
      html = new DOMParser().parseFromString(html, "text/html");
      document.getElementById('id_da_div_que_irá_receber').innerHTML = html.getElementById('id_da_div_que_de_onde_virá').innerHTML.trim();
   }
}
http.send(null);
  • Good morning, this code worked very well in firefox and even in explorer, but in CHROME it does not run at all. I already tried to know what could be but found no solution,have some hint of what might be the problem?

  • Now I figured out the problem,for security reasons Chrome requires the page to be with some protocol,in case,I hosted my pages on a test host and with https protocol,and it worked, anyway vlw

  • Oh yes, Chrome locks if it’s not HTTP...

  • Hello Luan! When online, please give a call. I wanted to ask a question. Thanks!

  • 'Cause no sam,?

  • Obg Luan, already solved. Thanks!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.