Get contents source code in Javascript

0

Hello,

I'm trying to use a javascript to read the source code of another page and write that content inside a div.

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

It's something like the file_get_contents of php but in javascript

It's not working ... Can you give me a light? Thank you !!

    
asked by anonymous 09.08.2018 / 23:43

1 answer

2

Missing jquery library

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scripttype="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>
    
10.08.2018 / 00:05