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

Problems using XMLHTTRequest

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a


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

P: n/a
>>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

P: n/a
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

P: n/a
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

P: n/a


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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.