Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > XMLHTTPRequest variable and callback function question

Reply
Thread Tools

XMLHTTPRequest variable and callback function question

 
 
chris.schwalm@gmail.com
Guest
Posts: n/a
 
      11-21-2005
This is part II of this <a
href="http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/cd54951e0ea277de/639c67f2f7cadb1f?tvc=2&q=Simple+HTML+read%2Fwrite+ question#639c67f2f7cadb1f">
post</a>. I am creating a java script that bascially reads a webpage -
forwards it to an external program/parser/servlet - then [locally]
overwrites the webpage with a slightly modify version.

Basically I have two questions:
(1) Do I need to set my XMLHTTPRequest variable to null after I am
finished using it? I would like to be as secure as possible. I have
seen this in some examples but have not been able to implement it.

If I do NOT need to set it to null then I am finished and you do not
need to read any more.

But if I do have to set it to null, please show me what I am doing
wrong. I think the best place to do that would be in the callback
function, but when i try this I get an error. Here is my code:

///////////////////////////////////////////////////////////
// Method will send the HTML source to a "parser"
// and post the reply
//////////////////////////////////////////////////////////
function post_plain(dom)
{

connection=false;
// We can cope with old IE versions.
// and security blocked creation of the objects.
try {
connection = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e){
try {
connection = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
connection = false;
}
}
if (!connection && typeof XMLHttpRequest!='undefined')
{
connection = new XMLHttpRequest();
}



if(connection) {
// set URL to connect to
var url_ps = "a_secret_address_to_a_secure_servlet";

// method for initializing a post
connection.open("POST", url_ps,true);

// send request header
connection.setRequestHeader("Content-Type", "text/xml");

// method for sending the request
connection.send(dom.body.innerHTML);

connection.onreadystatechange = function() {
if (connection.readyState == 4) {
dom.body.innerHTML = connection.responseText;

// ****** THIS DOES NOT WORK **********
// clean up
// var connection = null;
}
}
}
else{
alert("ERROR - CONNECT == FALSE");
}
}


Thanks for viewing my code. Please feel free to make any sort of
comments or corrections. If you need me to post any more information
just let me know.

 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-22-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> This is part II of this <a
>

href="http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/cd54951e0ea277de/639c67f2f7cadb1f?tvc=2&q=Simple+HTML+read%2Fwrite+ question#639c67f2f7cadb1f">
> post</a>.


a) HTML code won't work for this (otherwise the Google Groups
interface used would be very b0rken), so don't even try it.

b) Don't refer to threads to continue discussion, continue _them_.
(in Google Groups: click "Reply" below the respective article.)

> I am creating a java script that bascially reads a webpage -
> forwards it to an external program/parser/servlet - then [locally]
> overwrites the webpage with a slightly modify version.
>
> Basically I have two questions:
> (1) Do I need to set my XMLHTTPRequest variable to null after I am
> finished using it? I would like to be as secure as possible.


Then you probably have to nullify the reference, requesting later
Garbage Collection. It is known that IE's automatic Garbage Collection
is unreliable when the location of the displayed document changes.

> // ****** THIS DOES NOT WORK **********


"Does not work" is a useless error description. [psf 4.11]

<http://jibbering.com/faq/#FAQ4_43>

> // clean up
> // var connection = null;


Why, with using the `var' keyword you (re)declare the variable. Although I
cannot image why, if `var foo = null' does not work here, try `foo = null'.
This would be more reasonable, considering that you want to nullify the
_previous_ reference.


HTH

PointedEars
 
Reply With Quote
 
 
 
 
chris.schwalm@gmail.com
Guest
Posts: n/a
 
      11-22-2005
The problem isn't that the particular statement does not work. The
problem is that for some reason that statement seems to be getting
executed before the "dom.body.innerHTML = connection.responseText;"
statement completes. So I am getting an error that basically says I am
trying to work with data that is not there yet

 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      11-22-2005
Thomas 'PointedEars' Lahn said the following on 11/22/2005 8:24 AM:
> (E-Mail Removed) wrote:
>
>
>>This is part II of this <a
>>

>
> href="http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/cd54951e0ea277de/639c67f2f7cadb1f?tvc=2&q=Simple+HTML+read%2Fwrite+ question#639c67f2f7cadb1f">
>
>>post</a>.

>
>
> a) HTML code won't work for this (otherwise the Google Groups
> interface used would be very b0rken), so don't even try it.
>
> b) Don't refer to threads to continue discussion, continue _them_.
> (in Google Groups: click "Reply" below the respective article.)


No.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.


--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
 
Reply With Quote
 
web.dev
Guest
Posts: n/a
 
      11-22-2005

(E-Mail Removed) wrote:
> This is part II of this <a
> href="http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/cd54951e0ea277de/639c67f2f7cadb1f?tvc=2&q=Simple+HTML+read%2Fwrite+ question#639c67f2f7cadb1f">
> post</a>. I am creating a java script that bascially reads a webpage -
> forwards it to an external program/parser/servlet - then [locally]
> overwrites the webpage with a slightly modify version.
>
> Basically I have two questions:
> (1) Do I need to set my XMLHTTPRequest variable to null after I am
> finished using it? I would like to be as secure as possible. I have
> seen this in some examples but have not been able to implement it.
>
> If I do NOT need to set it to null then I am finished and you do not
> need to read any more.
>
> But if I do have to set it to null, please show me what I am doing
> wrong. I think the best place to do that would be in the callback
> function, but when i try this I get an error. Here is my code:
>
> ///////////////////////////////////////////////////////////
> // Method will send the HTML source to a "parser"
> // and post the reply
> //////////////////////////////////////////////////////////
> function post_plain(dom)
> {
>
> connection=false;
> // We can cope with old IE versions.
> // and security blocked creation of the objects.
> try {
> connection = new ActiveXObject("Msxml2.XMLHTTP");
> } catch (e){
> try {
> connection = new ActiveXObject("Microsoft.XMLHTTP");
> } catch (E) {
> connection = false;
> }
> }
> if (!connection && typeof XMLHttpRequest!='undefined')
> {
> connection = new XMLHttpRequest();
> }
>
>
>
> if(connection) {
> // set URL to connect to
> var url_ps = "a_secret_address_to_a_secure_servlet";
>
> // method for initializing a post
> connection.open("POST", url_ps,true);
>
> // send request header
> connection.setRequestHeader("Content-Type", "text/xml");
>
> // method for sending the request
> connection.send(dom.body.innerHTML);
>
> connection.onreadystatechange = function() {
> if (connection.readyState == 4) {
> dom.body.innerHTML = connection.responseText;
>
> // ****** THIS DOES NOT WORK **********
> // clean up
> // var connection = null;
> }
> }


Define the onreadystatechange function before you do a send. It
doesn't make sense for you to make a request and then set the
onreadystatechange.

> }
> else{
> alert("ERROR - CONNECT == FALSE");
> }
> }
>
>
> Thanks for viewing my code. Please feel free to make any sort of
> comments or corrections. If you need me to post any more information
> just let me know.


 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-22-2005
Randy Webb wrote:

> Thomas 'PointedEars' Lahn said the following on 11/22/2005 8:24 AM:
>> b) Don't refer to threads to continue discussion, continue _them_.
>> (in Google Groups: click "Reply" below the respective article.)

>
> No.
>
> If you want to post a followup via groups.google.com, don't use the
> "Reply" link at the bottom of the article. Click on "show options" at
> the top of the article, then click on the "Reply" at the bottom of the
> article headers.


Oh my! Thanks for notifying me, and thanks to myself for not using
Google Groups for posting


Regards,
PointedEars
 
Reply With Quote
 
chris.schwalm@gmail.com
Guest
Posts: n/a
 
      11-22-2005
ok i need help. this code works fine when i have it [hardcoded] on a
webpage as a javascript. it works find when i have it [hardcoded] on a
webpage as a bookmarklet. but it NEVER works when i save that
bookmarklet to my "favorites" and try to execute it from there. What is
the deal? And are there any good resources for the differences between
normal javascript and bookmarklet javascript?

var connection=false;
function post_me(dom){
try{
connection=new ActiveXObject('Msxml2.XMLHTTP');
}
catch(e){
connection=false;
}
if(connection){
var myUrl='_a_secret_http_servlet_address'';
var myStr=dom.body.innerHTML;
connection.onreadystatechange=alertContents;
connection.open('POST',myUrl,true);

connection.setRequestHeader('Content-type','application/x-www-form-urlencoded');
connection.setRequestHeader('Content-length',myStr.length);
connection.setRequestHeader('Connection','close');
connection.send(myStr);
}
else{
alert('ERROR-XMLHTTPRequest failed');
}
alert('very end');
}

function alertContents(){
if(connection.readyState==4){
if(connection.status==200){
result=connection.responseText;
document.body.innerHTML=result;
}
else{
alert('There was a problem with the request.');
}
}
}

and the only difference between this and the "bookmarklet" is that i
add a the function call "post_me(window.document)" function call at the
end and place it all in <a href="javascript: (within the webpage).

 
Reply With Quote
 
web.dev
Guest
Posts: n/a
 
      11-22-2005

(E-Mail Removed) wrote:
> ok i need help. this code works fine when i have it [hardcoded] on a
> webpage as a javascript. it works find when i have it [hardcoded] on a
> webpage as a bookmarklet. but it NEVER works when i save that
> bookmarklet to my "favorites" and try to execute it from there. What is
> the deal? And are there any good resources for the differences between
> normal javascript and bookmarklet javascript?


>From this website:


URL:http://subsimple.com/bookmarklets/rules.asp

"There is a limit to the number of characters your bookmarklet can
contain. The problem is this limit differs between browser versions.
For example, Internet Explorer 4 and 5 for Windows appear to be able to
handle up to 2083 characters, while Explorer 6.0 can only handle up to
508 characters - which is very bad news for bookmarklet developers!
Explorer 6.0 SP2 is even worse - it handles only up to 488 characters!"

Your bookmarklet can only be as long as the length of the URL address
the web browser can handle.

> var connection=false;
> function post_me(dom){
> try{
> connection=new ActiveXObject('Msxml2.XMLHTTP');
> }
> catch(e){
> connection=false;
> }
> if(connection){
> var myUrl='_a_secret_http_servlet_address'';
> var myStr=dom.body.innerHTML;
> connection.onreadystatechange=alertContents;
> connection.open('POST',myUrl,true);
>
> connection.setRequestHeader('Content-type','application/x-www-form-urlencoded');
> connection.setRequestHeader('Content-length',myStr.length);
> connection.setRequestHeader('Connection','close');
> connection.send(myStr);
> }
> else{
> alert('ERROR-XMLHTTPRequest failed');
> }
> alert('very end');
> }
>
> function alertContents(){
> if(connection.readyState==4){
> if(connection.status==200){
> result=connection.responseText;
> document.body.innerHTML=result;
> }
> else{
> alert('There was a problem with the request.');
> }
> }
> }
>
> and the only difference between this and the "bookmarklet" is that i
> add a the function call "post_me(window.document)" function call at the
> end and place it all in <a href="javascript: (within the webpage).


 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-22-2005
(E-Mail Removed) wrote:

> The problem isn't that the particular statement does not work. The
> problem is that for some reason that statement seems to be getting
> executed before the "dom.body.innerHTML = connection.responseText;"
> statement completes. So I am getting an error that basically says
> I am trying to work with data that is not there yet


First of all: sorry, I gave you wrong and probably misleading advice. In
Google Groups, do not use the Reply link below the article but use that
via Options as Randy said. "Continuing discussion" includes that it is
important that you quote what you are replying to and trim your quotes to
what you are replying to directly, so that readers see the context without
explicitly looking it up. Of course, best advice is not to use Google
Groups for posting but a decent newsreader program, Thunderbird or KNode,
for example.

As for the problematic nullification: you did not provide the error message
or any debug result I asked for, only your assumptions. However, since we
are talking about DOM features, it is entirely possible that the
AssignmentExpression above was already evaluated in the way that the change
to the DOM element was issued but not done yet. Although I do think
evaluation to connection.responseText would have taken place by at that
point and nullifying the `connection' reference directly afterwards should
not have posed any problem.

I suggest you try to reverse the order of statements for the definition of
the `onreadystatechange' handler and the send() method call as "web.dev"
suggested. If that still does not help and the nullification really is the
problem, you can still nullify the reference in the `onunload' handler of
the Window object.


HTH

PointedEars
 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      11-22-2005

web.dev wrote:
> Explorer 6.0 SP2 is even worse - it handles only up to 488 characters!


Holy... !?! Do you/anyone have a link to MSDN about it? This
effectively the end of GET method under IE if true.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I create a function in my library for passing user callback function Angus C Programming 32 04-15-2008 02:28 PM
Does changing a Tkinter Variable bound to a widget invoke its'command' callback function? execrable@gmail.com Python 0 11-29-2007 10:11 AM
can a class member function be used as a callback function? JDT C++ 6 03-29-2007 12:45 PM
Using function pointer in callback function pvdm C++ 1 09-09-2003 12:26 PM
Howto: Class member function as callback function for dialog box prettysmurfed C++ 6 07-22-2003 06:17 PM



Advertisments