0

I want to write a JavaScript to return contents of a webpage when URL is given and enter those contents as a data in table?

The alert pop up is not working.

Return HTML content as a string, given URL. JavaScript function returns a blank screen.

Here is my code:

<html>
<head></head>
<body>
<script type="text/JavaScript"> function httpGet(theUrl) {
    var xmlHttp = null;
    xmlHttp = new XMLHttpRequest();
    xmlHttp.open("GET", theUrl, false);
    xmlHttp.send(null);
    return xmlHttp.responseText;
}
document.write(httpGet("stackoverflow.com/")); </script>
</body>
</html>
DimaSan
  • 12,264
  • 11
  • 65
  • 75
sindhu
  • 21
  • 4
  • Possible duplicate of [Return HTML content as a string, given URL. Javascript Function](http://stackoverflow.com/questions/10642289/return-html-content-as-a-string-given-url-javascript-function) – DimaSan Oct 25 '16 at 08:32
  • You need to provide some code that you tried as well as errors or exceptions that you got. When you ask so general and wide questions users usually just downvote your question. I advise you to remove this question and create another one, more specific. – DimaSan Oct 26 '16 at 05:12
  • if you can't format your code, add it "as is" and someone here will edit your question and format it in a pretty way. Again I recommend you to create new question and delete this one, because it is already downvoted and very little users usually comes again after editing. – DimaSan Oct 27 '16 at 07:15

2 Answers2

0

You can simply get contents of a webpage by using javascript's ajax

var contents="";
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    contents=this.responseText;
  }
};
xhttp.open("GET", "webpage.html", true);
xhttp.send();

Although you can also manipulate HTML you should make sure that webpage.html is showing the data in a valid format for you to easily manipulate. Like JSON, xml or anything else so that you can use the data in the contents variable and iterate over it for your insert statements.

The SuperKat
  • 234
  • 2
  • 10
  • Just a couple of points, apparently if OP's question doesn't have a `jquery` tag, your answers are meant to be pure javascript. I won't mark you down, but I've a feeling some SO Elite's will. Also, just for the record HTML is also a parse-able format. – Keith Oct 25 '16 at 08:52
  • Thank you for not marking me down, I made Improvements :) – The SuperKat Oct 25 '16 at 09:08
  • inplace of webpage.html, can the URL be used?? – sindhu Nov 02 '16 at 04:47
  • That's Precisely it is for. – The SuperKat Nov 02 '16 at 12:59
  • you can quickly identify the error by opening your browser's console while running the script. An error will be thrown if there is some problem with the URL, or if your script itself. That way we can precisely know what is wrong :) – The SuperKat Nov 02 '16 at 13:02
0

The reason because its showing a blank screen is because of the cross origin policy. stackoverflow.com does not allow CORS(cross origin requests).

And one more thing,, Until and unless you are requesting from a relative Url, specify the address with the protocol name.

Like : 'stackoverflow.com' to 'http://stackoverflow.com'

So just change the address to your file and it will work fine. :)

The SuperKat
  • 234
  • 2
  • 10