Jason Kim Jason Kim - 8 months ago 61
HTML Question

Return HTML content as a string, given URL. Javascript Function

I want to write a javascript function that returns HTML content as string given URL to the function. I found a similar answer on Stackoverflow.

I am trying to use this answer to solve my problem.

However, it seems as though

isn't writing anything. When I load the page, I get a a blank screen.

<script type="text/JavaScript">
function httpGet(theUrl)
var xmlHttp = null;

xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false );
xmlHttp.send( null );
return xmlHttp.responseText;


you need to return when the readystate==4 e.g.

function httpGet(theUrl)
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
            return xmlhttp.responseText;
    xmlhttp.open("GET", theUrl, false );