Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 23rd, 2004, 08:32 AM
Registered User
 
Join Date: Oct 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Microsoft.XMLHTTP and javascript under IE6

Hello,

I use a check tool in a Javascript to get the HTTP status (200, 404, etc...) of severals pages (http://@IP/une_page.htm) in the to print it in a html page. This is working client side. Here is my code :

function checkURLStatus(url)
{
    var http = new ActiveXObject('Microsoft.XMLHTTP'); // or Msxml2.XMLHTTP.3.0
    http.open('HEAD', url, true); // Asynchronous request
    try
    {
        http.send();
        do
        {
            ... loop for response timeout ...
        }
        while(http.readyState != 4);
    }
    catch(erreur)
    {
        return 'Err';
    }
    return (http.status);
}

This used to work under IE5.x but it doesn't with IE6 (v6.0.2600).
The readyState always keep the value 1.

A synchronous request (false instead of true) works but if the requested server is down IE freezes about 20 seconds.

If someone have any idea ?

JFB
 
Old October 23rd, 2004, 10:48 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Works okay on my machine except it can take a while, what are you doing in the waiting loop?
In my opinion you are better off doing a true asynchronous call, try this example:
Code:
<html>
<head>
<title>Test URL</title>

<script type="text/Jscript">
  function checkURLStatus(url)
  {
      var http = new ActiveXObject('Msxml2.XMLHTTP.3.0'); // or ActiveXObject('Microsoft.XMLHTTP')
      http.open('HEAD', url, true); // Asynchronous request
      try
      {    
          http.send();
          do
          {
            for (var i = 0; i < 10000000; i++)
            {
              var a = i;
            }
          }
          while(http.readyState != 4);
      }
      catch(e)
      {
          return e.message;
      }
      return (http.getAllResponseHeaders());
  }

  function checkURLStatusAsync(url)
  {
      asyncHttp = new ActiveXObject('Msxml2.XMLHTTP.3.0');
      asyncHttp.open('HEAD', url, true); // Asynchronous request
      asyncHttp.onreadystatechange = checkReadyState;
      try
      {    
          asyncHttp.send();
      }
      catch(e)
      {
          return e.message;
      }
  }

  function checkReadyState()
  {
    var iState = asyncHttp.readyState;
    showData("Ready state: " + iState, true);
    if (iState == 4)
    {
      showData(asyncHttp.getAllResponseHeaders(), true);      
    }
  }

  function showData(Data, Append)
  {
    var oOutput = document.getElementById("txtResults");
    if (Append)
    {
      oOutput.value += Data + "\n";
    }
    else
    {
      oOutput.value = Data;
    }

  }

  function testUrlAsync(Url)
  {
    checkURLStatusAsync(Url);
  }

  function testUrl(Url)
  {    
    var sResult = checkURLStatus(Url);
    showData(sResult);
  }
</script>
</head>
<body>
<input type="text" size="40" value="http://www.google.com" id="txtUrl">
<input type="button" onclick="testUrl(txtUrl.value);" value="Test URL">&nbsp;&nbsp;
<input type="button" onclick="testUrlAsync(txtUrl.value);" value="Test URL async">
<br>
<textarea rows="30" cols="60" id="txtResults"></textarea>

</body>
</html>
--

Joe (Co-author Beginning XML, 3rd edition)
 
Old October 23rd, 2004, 05:06 PM
Registered User
 
Join Date: Oct 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Joe for your answer, it works at my home too.
You wanted to know all my code, here it is and I thik it is the problem :

function checkURLStatus(url)
{
    var http = new ActiveXObject('Microsoft.XMLHTTP');
    http.open('HEAD', url, true);
    try
    {
        http.send();
        var timeout = 4000; // 4 seconds
        var date = new Date();
        var start = date.getTime();
        do
        {
            var date = new Date();
            var now = date.getTime();
            var delta = now - start;
            if (delta > timeout)
            {
                http.abort();
                return 'Tmo';
            }
        }
        while(http.readyState != 4);
    }
    catch(erreur)
    {
        return 'Err';
    }
    return(http.status);
}

With the do..while loop, I can set the acceptable time to wait for the http response.
The function is called from an other one in a loop "for" and each loop has a different url as input. Why this is OK with IE5 and not with IE6 .... ?

A similar thing seams more difficult to do using the onReadyStateChange method and this is e new problem. I think setTimeouts or setInterval commands will be useful. Unless you know something for pausing, like sleep or delay...

JFB
 
Old October 24th, 2004, 03:27 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I don't know why IE6 differs from IE5, I don't have version 5 to test with.
Unfortunately although the ServerXmlHttp class has a waitForResponse method the standard version does not.
On my company intranet I use meadroid.com scriptX component for pausing. It also has some useful functions for printing and doing awkward JavaScript operations such as passing by reference.

--

Joe





Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript, Msxml2.XMLHTTP - Spanish Characters Pr itHighway Classic ASP Basics 1 November 14th, 2008 09:20 PM
Dynamically generating Javascript (XSLT in IE6) einszwei XSLT 1 June 28th, 2007 01:07 PM
XML Post using microsoft.XMLHTTP csmajor231 XML 0 April 5th, 2004 03:06 PM
Problem With Microsoft.XMLHTTP binod XSLT 2 August 13th, 2003 02:33 PM
Why does Microsoft.XMLHTTP not work? mega XML 2 August 12th, 2003 07:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.