Problems using XMLHTTRequest

W

Wescotte

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.responseText;

}
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>
 
M

Martin Honnen

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.responseText;

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.
 
W

Wescotte

Whether it is a good idea to fetch a complete HTML document and insert
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.

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?
 
W

Wescotte

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?
 
T

Thomas 'PointedEars' Lahn

Wescotte said:
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
 
M

Martin Honnen

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
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.
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.
 
T

Thomas 'PointedEars' Lahn

Martin said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top