0

i want get page code or page source from url with javascript , but my code didn't work. i need page source and then get element inner text
anyone have suggest ?

<html>
<head>

<script type="text/javascript">
function loadXMLDoc()
{

var xhr = new XMLHttpRequest();
xhr.onload = function() {
  console.log(this.responseXML.title);
}
xhr.open("GET", "http://www.bbc.com/");
xhr.responseType = "document";
xhr.send();
document.getElementById("myDiv").innerHTML = xhr.responseText;
  
};
 
</script>

</head>
<body>


<div id="myDiv"><h2>text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>
  • 1
    Open the Developer Tools in your browser. Look at the Console. **Read the error messages** – Quentin May 03 '18 at 10:16
  • Failed to load http://www.bbc.com/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:2390' is therefore not allowed access. – Mehrdad Dalvand May 03 '18 at 10:26
  • After reading error messages, try using a search engine to learn more about them. That will lead you to existing questions like [this one](https://stackoverflow.com/a/35553666/19068) – Quentin May 03 '18 at 10:29

0 Answers0