Note that there are some explanatory texts on larger screens.

plurals
  1. POjavascript connect to web site code not working
    primarykey
    data
    text
    <p>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?</p> <p>Html code:</p> <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;script src="Monitor.js" type="text/javascript"&gt;&lt;/script&gt; &lt;title&gt;Web Site Monitor&lt;/title&gt; &lt;/head&gt; &lt;body onload="setup()"&gt; &lt;div id="MonitorInformation"&gt;Connecting...wait&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Java script code:</p> <pre><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 ..."; } } } </code></pre> <p>EDIT 1: my fix using JSON</p> <pre><code>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; } }; } &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;script src="Monitor.js" type="text/javascript"&gt;&lt;/script&gt; &lt;title&gt;Web Site Monitor&lt;/title&gt; &lt;/head&gt; &lt;body onload="setup()"&gt; &lt;div id="MonitorInformation"&gt;Connecting...wait&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>thanks in advance, George</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload