473,396 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

XMLHttpRequest too slow

I have a form validation function that is supposed to use
XMLHttpRequest to validate a url (i.e. make sure it returns a 200), but
the problem is sometimes it takes about 5 seconds to return anything
and sometimes it returns right away with the wrong response.

What am I doing wrong? Or is it something other than the javascript
maybe? Thanks.

function validate(myform){
//Verify if URLs are valid.
var page1 = document.getElementById('page1');
var error = '';
var xmlhttp = false;
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(E){
xmlhttp = false;
}
}
if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
xmlhttp = new XMLHttpRequest();
}
xmlhttp.open("HEAD",page1.value,true);
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
if(xmlhttp.status != 200){
error = "Invalid URL";
}
}
}
xmlhttp.send(null);
if(error && error != ''){
alert(error);
return false;
}else{
return true;
}
}

Jul 23 '05 #1
3 8737
"otto" <ot****@pegasi.net> writes:
I have a form validation function that is supposed to use
XMLHttpRequest to validate a url (i.e. make sure it returns a 200), but
the problem is sometimes it takes about 5 seconds to return anything
and sometimes it returns right away with the wrong response.
.... xmlhttp.open("HEAD",page1.value,true);
The third argument, true, means that the connection will be
asynchroneous. That means that calling "send" will return
immediately, and not wait for the communication to finish.
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
if(xmlhttp.status != 200){
error = "Invalid URL";
}
}
}
xmlhttp.send(null);
if(error && error != ''){


Since communication is asynchroneous, it's probably not done
talking to the server yet when this code is executed. In fact,
since Javascript is generally not multithreaded, even if it is
done, the readystatechange handler has not been executed yet.

I can't see where the 5 second delay comes from in this code.
It could just be the browser hanging for some reason.

(btw, "error && error != ''" is superfluous, since '' counts as false.
Just "error" will do).
You could do a synchroneous connection instaead (false as third
argument to "open"). The there won't be any readystatechange events,
but the call to "send" will only return when the answer is ready.
---
xmlhttp.open("HEAD",page1.value,false);
xmlhttp.send("");
error = (xmlthhp.status == 200) ? "" : "Illegal URL";
---

Regards
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #2


otto wrote:

xmlhttp.open("HEAD",page1.value,true);
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
if(xmlhttp.status != 200){
error = "Invalid URL";
}
}
}
xmlhttp.send(null);
if(error && error != ''){
alert(error);
return false;
}else{
return true;
}


It doesn't make sense to have code here after the send() call checking
error, you should call the send method and any code that has to happen
once the request has been made and the response has been sent should go
into the onreadystatechange handler.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #3
This is great. That's exactly what I needed to know. Thanks to you and
Martin for the assistance.

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Marco Laponder | last post by:
Hi All, I am using XmlHttpRequest object within Firefox to get a xml document from the servlet. The reponseText is set but the responseXml is null. My Code is: req = new XMLHttpRequest();...
10
by: Matt Kruse | last post by:
I'm aware of the circular reference memory leak problem with IE/closures. I'm not sure exactly how to resolve it in this situation. Also, Firefox appears to grow its memory size with the same code....
13
by: TLaufenberg | last post by:
I'm new to Javascript programming and I've run into a bit of a snag with making an XMLHttpRequest in the Safari browser. Actually, the request doesn't work in Firefox either but only when I use a...
5
by: Peter Michaux | last post by:
Hi, The FAQ correctly says the following: "Mozilla (NN6.2+, Firefox, Ice Weasle etc), Opera 7.6+, Safari1.2+, the Windows version of IE versions 5+, and some other browsers provide the XML...
1
by: geevaa | last post by:
http://www.phpbuilder.com/columns/kassemi20050606.php3 XMLHttpRequest and AJAX for PHP programmers James Kassemi Introduction: Although the concept isn't entirely new, XMLHttpRequest...
1
by: Tarik Monem | last post by:
OK, I'm pretty sure this cannot work because I'm trying to use JavaScript (client-side) to write to an xml file (which is server-side) using XMLHttpRequest. Can I use PHP do what I'm trying to do?...
6
by: Patrick Nolan | last post by:
I'm working on cross-platform portability of some javascript. My Macintosh testing platform is rather old. It has Safari 1.3.2 and Internet Explorer 5.2. I got Safari working, but now IE is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.