473,378 Members | 1,411 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,378 software developers and data experts.

Problems using XMLHTTRequest

I've wrote a little test app to use XMLHTTPRquest test app but I'm
looking to add some functionality and I'm not quite so how to go about
doing it.

Below is the source

What I'd really like is to be able to use <SPAN onClick =
"Select(this);">

where I could pass the object and not have to display the results of
the XMLHTTPRequest via a getElementById(). Any ideas how I can create
the ability to pass an object ot the onreadystatechange function?

<HTML> <HEAD>

<script type="text/javascript">

function Request_URL(url)
{
// code for Mozilla, etc.
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=processStateChange;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
// code for IE
else if (window.ActiveXObject) {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
if (xmlhttp) {
xmlhttp.onreadystatechange=processStateChange;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
}
}

function processStateChange() {
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4) { // This means the request was sent and
the ENTIRE responce has been recieve (the whole page loaded)
if (xmlhttp.status==200) { // Check that the data was recieved
correctly
document.getElementById("T1").innerHTML=xmlhttp.re sponseText;

}
else {
alert("Problem retrieving XML data");
}
}
}

function Select()
{
var url = "HTTPRequest_file.html";
var temp=Request_URL(url);
}

</script>
</HEAD>
<BODY>
<SPAN id = "T1" onClick = "Select();">
Click on me to update my text from another webpage
</SPAN>

</BODY>
</HTML>

Jan 24 '06 #1
6 2560


Wescotte wrote:

What I'd really like is to be able to use <SPAN onClick =
"Select(this);">
function Request_URL(url)
function Request_URL(url, elementToChange)
xmlhttp.onreadystatechange=processStateChange;
xmlhttp.onreadystatechange = function () {
processStateChange(elementToChange);
};

xmlhttp.onreadystatechange=processStateChange;
Same as above.
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
}
}

function processStateChange() {
function processStateChange (elementToChange)

document.getElementById("T1").innerHTML=xmlhttp.re sponseText;
elementToChange.innerHTML = ...

function Select()
function Select (elementToChange)
var temp=Request_URL(url);
Request_URL(url, elementToChange)
<SPAN id = "T1" onClick = "Select();">


<span onclick="Select(this);">

Whether it is a good idea to fetch a complete HTML document and insert
it into a span? Oh well. I hope that HTTPRequest_file.html is only a
snippet of HTML that fits into a span.

And the code could take more fixes, creating xmlhttp with a side effect
as a global variable is not a good style.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jan 24 '06 #2
>>Whether it is a good idea to fetch a complete HTML document and insert
it into a span? Oh well. I hope that HTTPRequest_file.html is only a
snippet of HTML that fits into a span.
I was simply using the span to test XMLHTTPRequest however in this case
my HTTPRequest_file.html is simply <HTML> <BODY> <P ID =
'DATA_TO_READ'> This is a test </p> </BODY> </HTML>

I wasn't sure if I could do something like alert(
xmlhttp.responceText.getIdentifyById("DATA_TO_READ ") ); and was please
to find out you could.
And the code could take more fixes, creating xmlhttp with a side effect
as a global variable is not a good style.


I'm not sure I follow this second part. What exactly do you mean
creating xmlhttp with a side effect as a global?

Do you mean the xmlhttp=new and not keeping track of the xmlhttp
object?

I was planning somewhere in the processStateChange after reading the
final result I to delete xmlhttp

But with javascript does xmlhttp become global? or is it simply passed
on the processStateChange() call?

The reason I ask is I intend to have mutliple requests going and it's
very likely they would occur at the same time. Would it be a better
idea to have say queue to store the XMLHTTPRequest pointers and then
erase them as they complete?

Jan 24 '06 #3
Err let me clarify what I was asking about as I'm very new to
JavaScript

function MyFunction()
{
var my_new_object = new OBJECT_TYPE;
}
once MyFunction() terminates the variable pointing to my new
OBJECT_TYPE is out of scope correct? Or does my_new_object become
global?

If my assume is correct and I use a similar method as to how you
explained above I can simply delete my XMLHTTPRequest object after I
process it's result in the onreadystatechange function correct?

I guess what I'm really confused about is how the function
onreadystatechange (in my case above function processStateChange() )
knows the variable name for the XMLHTTPRequest object? In the case
above it uses xmlhttp which I'm not sure why is valid in the function
scope.

After looking at it again I can only assume that xmlhttp=new OBJ
actually makes it global?

If so how can I create multiple instances of this object?

Jan 24 '06 #4
Wescotte wrote:
function MyFunction()
{
var my_new_object = new OBJECT_TYPE;
}
once MyFunction() terminates the variable pointing to my new
OBJECT_TYPE is out of scope correct?
Correct, and the object it referred to is subject to garbage collection.
Or does my_new_object become global?
No, it does not, because it was declared locally.
If my assume is correct and I use a similar method as to how you
explained above I can simply delete my XMLHTTPRequest object after I
process it's result in the onreadystatechange function correct?
Incorrect. You can only make an object subject to garbage collection (by
assigning `null' or apply `delete' to all its references) which will delete
it and free the respective memory _when appropriate_. Since XMLHTTPRequest
objects are host objects, the rules that apply for native objects do not
necessarily apply for the former. In fact, it is error-prone to try on
them what wass described before; you should let the GC do its work when the
variable goes out of scope instead.
[...]
After looking at it again I can only assume that xmlhttp=new OBJ
actually makes it global?
If the `xmlhttp' variable was not locally declared before, yes. And you
would be right.
If so how can I create multiple instances of this object?


You already do with the NewExpression. However, with one reference you can
only have access to one object (instance) directly. Simple solution: more
named references and more NewExpressions used to create them, where each
value is a reference to a different object.
PointedEars
Jan 25 '06 #5


Wescotte wrote:

I was simply using the span to test XMLHTTPRequest however in this case
my HTTPRequest_file.html is simply <HTML> <BODY> <P ID =
'DATA_TO_READ'> This is a test </p> </BODY> </HTML>
And that does not make sense to me as you then set the innerHTML of a
span element to that markup. Would you put a complete
<html><body><p>...</p></body></html> inside of a span with static HTML
markup? Hopefully not.
I wasn't sure if I could do something like alert(
xmlhttp.responceText.getIdentifyById("DATA_TO_READ ") ); and was please
to find out you could.


Not sure what you actually tried, but
xmlhttp.responceText
is certainly not definied, and even
xmlhttp.responseText.getIdentifyById
looks not like anything going to work, responseText is a string and does
not have any method named getIdentifyById.
And the code could take more fixes, creating xmlhttp with a side effect
as a global variable is not a good style.

I'm not sure I follow this second part. What exactly do you mean
creating xmlhttp with a side effect as a global?


Well anytime you do e.g.
varName = someExpression
and you haven't declared varName then the variable named varName is
created as a global variable.
Thus if you really want a global variable named
xmlhttp
then declare it properly with
var xmlhttp;
outside of any function code.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jan 25 '06 #6
Martin Honnen wrote:
Well anytime you do e.g.
varName = someExpression
and you haven't declared varName then the variable named varName is
created as a global variable.


To be exact, it is globally available, but it is not necessarily a global
variable, that is, a property of the Global Object. It can also be
considered a reference to a property of another object in the scope chain
and the assignment can lead to an error if that object is a host object
(as it is in IE). Hence the warning in the JavaScript/Error Console of
Gecko-based UAs.
PointedEars
Jan 25 '06 #7

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

Similar topics

0
by: Jerome Lefebvre | last post by:
Hello, Hope this will interest a few. I been working with a friend on the problems given out during the "International Collegiate Programming Contest" (ICPC) http://icpc.baylor.edu/icpc/ ....
14
by: Jim Hubbard | last post by:
Are you up to speed on the difficulties in using the 1.1 .Net framework? Not if you are unaware of the 1,596 issues listed at KBAlertz (http://www.kbalertz.com/technology_3.aspx). If you are...
26
by: jamesbeswick | last post by:
I've been using Access since version 97 and I've migrated to 2003. I've noticed a substantial number of strange ActiveX/OLE and code corruption problems when writing databases. The only solution...
10
by: BBFrost | last post by:
We just recently moved one of our major c# apps from VS Net 2002 to VS Net 2003. At first things were looking ok, now problems are starting to appear. So far ... (1) ...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
0
by: Peter R. Vermilye | last post by:
I am involved on a web application that is using a third party set of APIs for remote database access (middleware). I've been brought in because of my background in programming, thus I'm new to...
3
by: Andreas | last post by:
Hi! I'm currently developing a DLL that makes use of C++ and .net (mixed) using Visual Studio 2003. Now, as I wanted to move to the new Visual Studio 2005, I converted this project into the...
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
9
by: =?Utf-8?B?SG93YXJkIFNtaXRo?= | last post by:
I am using VC++ 6.0 (with SP5 installed). When using WinXP this is with SP2 installed. I am developing an instrumentation system comprising a set of networked PCs connected using TCP/IP TCP links....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?

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.