By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,642 Members | 1,106 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,642 IT Pros & Developers. It's quick & easy.

sleep() || settimeout()

P: 3
I`m writing script that makes HEAD request to multiple web sites and *multiple* is the problem.
If i`m using
xmlhttp.open("HEAD", url,true) script shows actual status only for last array object, but if i use false script end after first unsuccessful request.
Does anyone have any idea how to workaround that problem or make script better.
Thanks!

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" language="javascript">
  2. var xmlhttp=false;
  3. /*@if (@_jscript_version >= 5)
  4.  try {
  5.   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  6.  } catch (e) {
  7.   try {
  8.    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  9.   } catch (E) {
  10.    xmlhttp = false;
  11.   }
  12.  }
  13. @end @*/
  14. if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  15.     try {
  16.         xmlhttp = new XMLHttpRequest();
  17.     } catch (e) {
  18.         xmlhttp=false;
  19.     }
  20. }
  21. if (!xmlhttp && window.createRequest) {
  22.     try {
  23.         xmlhttp = window.createRequest();
  24.     } catch (e) {
  25.         xmlhttp=false;
  26.     }
  27. }
  28.  
  29. function larray() {
  30. var arr = new Array();
  31. arr["1"] = "www.google.com";
  32. arr["2"] = "www.kernel.org";
  33. arr["3"] = "xxx";
  34. arr["4"] = "yyy";
  35. for(var i in arr){
  36. getans(arr[i],i);
  37. }}
  38.  
  39. function getans(url,i) {
  40. xmlhttp.open("HEAD", url,true);
  41.  xmlhttp.onreadystatechange=function() {
  42.   if (xmlhttp.readyState!=4) {
  43.     document.getElementById('l'+i).innerHTML="Processing..."
  44.   }
  45.    if (xmlhttp.readyState==4) {
  46.    if (xmlhttp.status==200)
  47.    {
  48.     document.getElementById('l'+i).innerHTML="Ok!"
  49.   }
  50.     else if (xmlhttp.status==404)
  51.     {
  52.      document.getElementById('l'+i).innerHTML="Not ok!"
  53.      }
  54.      else
  55.      {
  56.       document.getElementById('l'+i).innerHTML="Problem"
  57.      }
  58.   }
  59. }
  60.  xmlhttp.send(null)
  61.  }
  62. </script>
  63. <body onload="larray()">
  64. <p><b>1:</b> <span id="l1"></span></p>
  65. <p><b>2:</b> <span id="l2"></span></p>
  66. <p><b>3:</b> <span id="l3"></span></p>
  67. <p><b>4:</b> <span id="l4"></span></p>
  68.  
Feb 14 '07 #1
Share this Question
Share on Google+
4 Replies

acoder
Expert Mod 15k+
P: 16,027
true means asynchronous while false means synchronous, so what you've described is correct.

Maybe you need to create a new HTTPRequest. Put the code into a function and create a new HTTP request for each array item.
Feb 14 '07 #2

P: 3
Yes thanks but i found that out by my self yesterday. Make request for each array item works fine but in synchronous mode only.
Now i`m dealing with other problems, like best way to parse array elements for request and script works on locale machine fine but when i put script on server and accessing it script ends with error.
Feb 15 '07 #3

acoder
Expert Mod 15k+
P: 16,027
Post your code.
Feb 15 '07 #4

P: 3
Only few html tags support onload method. Those are:
Expand|Select|Wrap|Line Numbers
  1. <body>
  2. <frame>
  3. <frameset>
  4. <iframe>
  5. <img>
  6. <link>
  7. <script>
  8.  
I decide to use img tag:
Expand|Select|Wrap|Line Numbers
  1. <p><img src="20.gif" onload="getans('http://kernel.org',2)"><b>2:</b> <span id="l2"></span></p>
All code:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" language="javascript">
  2. var xmlhttp=false;
  3. /*@if (@_jscript_version >= 5)
  4.  try {
  5.   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  6.  } catch (e) {
  7.   try {
  8.    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  9.   } catch (E) {
  10.    xmlhttp = false;
  11.   }
  12.  }
  13. @end @*/
  14. if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  15.     try {
  16.         xmlhttp = new XMLHttpRequest();
  17.     } catch (e) {
  18.         xmlhttp=false;
  19.     }
  20. }
  21. if (!xmlhttp && window.createRequest) {
  22.     try {
  23.         xmlhttp = window.createRequest();
  24.     } catch (e) {
  25.         xmlhttp=false;
  26.     }
  27. }
  28.  
  29. function getans(url,i) {
  30. xmlhttp.open("HEAD", url,false);
  31.  xmlhttp.onreadystatechange=function() {
  32.   if (xmlhttp.readyState!=4) {
  33.     document.getElementById('l'+i).innerHTML="<img src='loading.gif'>"
  34.   }
  35.    if (xmlhttp.readyState==4) {
  36.    if (xmlhttp.status==200)
  37.    {
  38.     document.getElementById('l'+i).innerHTML="<img src='answer_good.gif'>"
  39.   }
  40.     else if (xmlhttp.status==404)
  41.     {
  42.      document.getElementById('l'+i).innerHTML="<img src='answer_bad.gif'>"
  43.      }
  44.      else
  45.      {
  46.       document.getElementById('l'+i).innerHTML="<img src='answer_bad.gif'>"
  47.      }
  48.   }
  49. }
  50.  xmlhttp.send(null)
  51.  }
  52. </script>
  53. <body>
  54. <p><img src="20.gif" onload="getans('yyy',1)"><b>1:</b> <span id="l1"></span></p>
  55. <p><img src="20.gif" onload="getans('http://kernel.org',2)"><b>2:</b> <span id="l2"></span></p>
  56. <p><img src="20.gif" onload="getans('http://google.com',3)"><b>3:</b> <span id="l3"></span></p>
  57. <p><img src="20.gif" onload="getans('xxx',4)"><b>4:</b> <span id="l4"></span></p>
  58. </body>
  59.  
On my machine code works great, but due browser security and other things script does not work on other machines and other browsers thats why i decide to write small program in java instead of using this script and dealing with all those things why script isn`t working.
Feb 19 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.