Get Contents source code in Javascript

Asked

Viewed 379 times

0

Hello,

I am trying to use a javascript to read the source code of another page and write this content inside a div.

<script type="text/javascript">
    $.get('teste.php', function(data) {
    document.getElementById('somediv').innerHTML = data;        
});
</script>

It’s something like the php file_get_contents but in javascript

It’s not working... Can I get a light? Thank you!!

  • is missing library

1 answer

2


Missing the jquery library

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

<script type="text/javascript">
    $.get('teste.php', function(data) {
    document.getElementById('somediv').innerHTML = data;        
});
 </script>

<div id="somediv"></div>

With pure Javascript

function makeHttpObject() {
  try {return new XMLHttpRequest();}
  catch (error) {}
  try {return new ActiveXObject("Msxml2.XMLHTTP");}
  catch (error) {}
  try {return new ActiveXObject("Microsoft.XMLHTTP");}
  catch (error) {}

  throw new Error("Não foi possível criar o objeto de solicitação HTTP.");
}
var request = makeHttpObject();
request.open("GET", "URL_da_pagina", true);
request.send(null);
request.onreadystatechange = function() {
  if (request.readyState == 4)
    document.getElementById('somediv').innerHTML = (request.responseText);
};

HTML

<div id="somediv"></div>

Browser other questions tagged

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