George2
George2

Reputation: 45801

javascript connect to web site code not working

Here is my javascript code which ping Google for every 10 seconds and display the connection status to html MonitorInformation element. But when I click the html file to debug, the information displayed at MonitorInformation element is always "Connecting...wait". I have debugged for some time but can not figure out. Any ideas what is wrong with my code?

Html code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="Monitor.js" type="text/javascript"></script>
    <title>Web Site Monitor</title>
</head>
<body  onload="setup()">
<div id="MonitorInformation">Connecting...wait</div>
</body>
</html>

Java script code:

function setup() {
    window.setInterval(PingWebSite, (10 * 1000));
}

function PingWebSite() {
    conObj = new ActiveXObject("Msxml2.XMLHTTP");
    conObj.open("GET", "http://www.google.com", true);
    conObj.onreadystatechange = function() {
    if (conObj.readyState === 4) {
        if (conObj.status === 200) {    
                loading.innerText = "Service is available";             
            } else {
                MonitorInformation.innerText = "Service is not available";
            }
        } else {
            MonitorInformation.innerText = "Connecting to www.google.com ...";
        }
    }
}

EDIT 1: my fix using JSON

function setup() {
    window.setInterval(PingWebSite, (10 * 1000));
}

function PingWebSite() {

    var http_request = new XMLHttpRequest();
    http_request.open("GET", "http://www.google.com", true);
    http_request.send(null);
    http_request.onreadystatechange = function() {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                MonitorInformation.innerText = "Connection ok";
                alert("ok");
            } else {
            MonitorInformation.innerText = "Connection fail";
            alert("fail");
            }
            http_request = null;
        }
    };
}

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="Monitor.js" type="text/javascript"></script>
    <title>Web Site Monitor</title>
</head>
<body  onload="setup()">
<div id="MonitorInformation">Connecting...wait</div>
</body>
</html>

thanks in advance, George

Upvotes: 0

Views: 4531

Answers (4)

Timmy
Timmy

Reputation: 11

Could it be that you forgot a semicolon there? E.g.: ...body onload="setup();"...

Upvotes: 1

FlySwat
FlySwat

Reputation: 175733

You can do this using a hidden iFrame to get around the cross domain limitations. A quick example to get you started:

(HTML Snippet)

<body>
    <div style="display: none;">
        <iframe id='hiddenFrame'></iframe>
    </div>
</body>

Javascript:

function setup()
{
    var iFrame = document.getElementById('hiddenFrame');
    var changeEvent = function () 
    { 
        loading.innerText = "Service is Available";
    }
    // IE
    iFrame.onload = changeEvent;
    // Firefox
    iFrame.onreadystatechange = changeEvent;

    setInterval(PingWebSite, (10 * 1000));
}

function PingWebSite() {
    var iFrame = document.getElementById('hiddenFrame');
    iFrame.src = 'http://www.google.com';
}

Upvotes: 1

AutomatedTester
AutomatedTester

Reputation: 22438

You can't connect to a site outside of your URL. Unless your code is on the google.com domain it won't work.

This is a browser security item called 'Same origin policy' http://en.wikipedia.org/wiki/Same_origin_policy

If you want to do cross site calls like that you will have to use JSONP http://en.wikipedia.org/wiki/JSON as that lets you do that.

Upvotes: 2

meder omuraliev
meder omuraliev

Reputation: 186742

You can't cross domain XHR for which you don't have access to AFAIK.

Upvotes: 1

Related Questions