|
From: Wescotte on 24 Jan 2006 14:15 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>
From: Martin Honnen on 24 Jan 2006 15:00 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. -- Martin Honnen http://JavaScript.FAQTs.com/
From: Wescotte on 24 Jan 2006 15:35 >>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?
From: Wescotte on 24 Jan 2006 15:52 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?
From: Thomas 'PointedEars' Lahn on 24 Jan 2006 20:01 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
|
Next
|
Last
Pages: 1 2 Prev: passing a variable Next: set dom element class (if element was dynamically created) |