Jason Kim Jason Kim - 1 year ago 119
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;

Answer Source

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 );
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download