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

XMLHTTPRequest variable and callback function question

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

Nov 23 '05 #1
Share this Question
Share on Google+
20 Replies


P: n/a
ch***********@gmail.com 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
Nov 23 '05 #2

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

Nov 23 '05 #3

P: n/a
Thomas 'PointedEars' Lahn said the following on 11/22/2005 8:24 AM:
ch***********@gmail.com 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/
Nov 23 '05 #4

P: n/a

ch***********@gmail.com 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.


Nov 23 '05 #5

P: n/a
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
Nov 23 '05 #6

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

Nov 23 '05 #7

P: n/a

ch***********@gmail.com 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).


Nov 23 '05 #8

P: n/a
ch***********@gmail.com 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
Nov 23 '05 #9

P: n/a
VK

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.

Nov 23 '05 #10

P: n/a
Through debugging other problems that i had, i fixed the "null" problem
that I mentioned earlier. Thanks for your help and the tips on posting.
Also I have modified the order of the callback function declaration.

Thomas 'PointedEars' Lahn wrote:
As for the problematic nullification: you did not provide the error message
or any debug result I asked for, only your assumptions.

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.


Nov 23 '05 #11

P: n/a
thanks for the quick reply.....I am using Explore 6.0 so maybe that is
the problem. Is there a place where this kind of imformation is stored?
Maybe I will try testing with an older version of IE. thanks again

Nov 23 '05 #12

P: n/a
ch***********@gmail.com wrote:
Through debugging other problems that i had, i fixed the "null" problem
that I mentioned earlier.
Care to elaborate on what really caused the problem?
Thanks for your help and the tips on posting.
You're welcome.
Also I have modified the order of the callback function declaration.
ACK, however that is no callback, it is an event handler.
Thomas 'PointedEars' Lahn wrote:
As for the problematic nullification: you did not provide the error
message or any debug result I asked for, only your assumptions.

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.


Please do not top-post, this is not Jeopardy[tm].

<URL:http://jibbering.com/faq/faq_notes/pots1.html>
PointedEars
Nov 23 '05 #13

P: n/a
Care to elaborate on what really caused the problem?

Truthfully, I am not exactly sure what caused/solved the problem. It
was just one of those things that fixed itself while I was messing with
other things.

Also I have modified the order of the callback function declaration.


ACK, however that is no callback, it is an event handler.

In this particular case, what is the difference between what I have
done and a "real" callback function? What I was trying to do was create
a function that gets called when the the "servlet" that I POST
information to responds. Said another way: I want my "alertConnects"
function to get called when I get a response from the
"connections.send(...)". Did i do this incorrectly?

Also, are there any reason that you can see as to why this javascript
wont convert into a bookmarklet. Even if I slim it down and keep the
characture count low, it still doesnt seem to work as a
bookmarklet...only as a java script (but maybe that is another post in
itself).

Thanks again

Nov 23 '05 #14

P: n/a
ch***********@gmail.com wrote:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Please provide attribution of quoted material.
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
Care to elaborate on what really caused the problem? Truthfully, I am not exactly sure what caused/solved the problem. It
was just one of those things that fixed itself while I was messing with
other things.


OK :)
> Also I have modified the order of the callback function declaration.


ACK, however that is no callback, it is an event handler.


In this particular case, what is the difference between what I have
done and a "real" callback function?


I understand callback as executable code (here: a Function object reference)
that is passed as a parameter to other code (here: as argument for another
function when it is called). An assigned event listener does not qualify
as such.
What I was trying to do was create a function that gets called when the
the "servlet" that I POST information to responds. Said another way: I
want my "alertConnects" function to get called when I get a response
from the "connections.send(...)".
Yes, but still not something I would call a callback.
Did i do this incorrectly?
No, you did it right.
Also, are there any reason that you can see as to why this javascript
wont convert into a bookmarklet. Even if I slim it down and keep the
characture count low, it still doesnt seem to work as a ^^^^^^^^^^^^^^^^^^^ bookmarklet... only as a java script (but maybe that is another post in ^^^^^^^^^^^ itself).


1. Java != JavaScript
2. <URL:http://jibbering.com/faq/#FAQ4_43>
3.
<URL:http://www.mozilla.org/projects/security/components/same-origin.html>
HTH

PointedEars
Nov 23 '05 #15

P: n/a
> 1. Java != JavaScript

yes, i realize this.
3.
<URL:http://www.mozilla.org/projects/security/components/same-origin.html>


I'm not sure if I fully grasp what is being said here. Does this mean
that if my bookmarklet/javascript [see source code above] is being
executed on a page that has a different origin then the servlet that i
am sending the source code to (and excepting a result back from) then
it will fail??

"script loaded from one origin from getting or setting properties of a
document from a different origin"

so you can only do XMLHTTPRequest "get" and "post" on pages that are of
the same origin?

Nov 23 '05 #16

P: n/a
VK

ch***********@gmail.com wrote:
so you can only do XMLHTTPRequest "get" and "post" on pages that are of
the same origin?


It's always some good news every day, is it? ;-)

Yes, IXMLHTTPRequest (Microsoft MSXML) and XMLHttpRequest (others) is
in the "same domain" security sandbox.

<http://msdn.microsoft.com/library/en-us/xmlsdk/html/9ad3c67d-6695-42cf-95fb-0310d0950a06.asp?frame=true>

<http://developer.mozilla.org/en/docs/Accessing_Web_Services_in_Mozilla_Using_WSDL_Proxy ing#The_Security_Model>

etc.

I'm not sure about RSS feeds is they can be used to bypass this
limitation. Someone more knowledgable may answer.

Nov 23 '05 #17

P: n/a

VK wrote:
ch***********@gmail.com wrote:
so you can only do XMLHTTPRequest "get" and "post" on pages that are of
the same origin?


It's always some good news every day, is it? ;-)

Yes, IXMLHTTPRequest (Microsoft MSXML) and XMLHttpRequest (others) is
in the "same domain" security sandbox.


BUT.....if it works as a javascript embeded on the webpage....it should
also work as a bookmarklet too...right? My problem is that if I have a
bookmarklet encoded in a webpage, and execute it from that webpage, IT
WORKS. But if I try to install that same bookmarklet into my browser's
"favorites" and execute it from there (which is the normal procedure
for bookmarklets), it DOES NOT WORK!

Any other ideas?

Nov 28 '05 #18

P: n/a
ch***********@gmail.com wrote:
VK wrote:
ch***********@gmail.com wrote:
> so you can only do XMLHTTPRequest "get" and "post" on pages that are of
> the same origin?
It's always some good news every day, is it? ;-)

Yes, IXMLHTTPRequest (Microsoft MSXML) and XMLHttpRequest (others) is
in the "same domain" security sandbox.


BUT.....if it works as a javascript embeded on the webpage....it should
also work as a bookmarklet too...right?


It is not right per se, that is a different environment.
My problem is that if I have a bookmarklet encoded in a webpage, and
execute it from that webpage, IT WORKS.
It is not executed as a bookmarklet there but as J(ava)Script code.
But if I try to install that same bookmarklet into my browser's
"favorites" and execute it from there (which is the normal procedure
for bookmarklets), it DOES NOT WORK!
No need to SHOUT! All posters in this thread have understood
your problem. You have not understood how bookmarklets work.
Any other ideas?


Google is your friend. [psf 6.1]

<URL:http://www.squarefree.com/bookmarklets/remotecontrol.html>
HTH

PointedEars
Nov 28 '05 #19

P: n/a
> BUT.....if it works as a javascript embeded on the webpage....it should
also work as a bookmarklet too...right? My problem is that if I have a
bookmarklet encoded in a webpage, and execute it from that webpage, IT
WORKS. But if I try to install that same bookmarklet into my browser's
"favorites" and execute it from there (which is the normal procedure
for bookmarklets), it DOES NOT WORK!

Any other ideas?


ok, I finally solved the problem. There were two seperate issues (1) As
mentioned above, IE has a very small char max for bookmarklets, and one
also needs to keep in mind that a whitespace is replaced with "%20". So
once I slimmed it way down things started working. (2) For this to work
on other browsers i had to add the following line:
"netscape.security.PrivilegeManager.enablePrivileg e('UniversalBrowserRead');"
and then things started working.

One thing that I am still uneasy about is how i got around the
"same-domain security policies" issues....maybe this will pop up
later???

Thanks for everyones help. I will post the working code just for
future users, this is the more robust version....(before I slimmed it
down for the actual bookmarklet):
function parseAndRepost(dom){
conn = false;
try{
// code for IE
if (window.ActiveXObject) {
conn=new ActiveXObject('Msxml2.XMLHTTP');
}
// code for Mozilla, etc.
else if (window.XMLHttpRequest) {
try {

netscape.security.PrivilegeManager.enablePrivilege ('UniversalBrowserRead');
}
catch (e) {
alert('Permission UniversalBrowserRead denied.');
}
conn=new XMLHttpRequest();
}
}
catch(e){
conn=false;
}

if(conn) {
var myUrl='http://____some__address___';
var myStr=dom.body.innerHTML;
conn.onreadystatechange=callback;
conn.open('POST',myUrl,true);

conn.setRequestHeader('Content-type','application/x-www-form-urlencoded');
conn.setRequestHeader('Content-length',myStr.length);
conn.setRequestHeader('Connection','close');
conn.send(myStr);
}
else{
alert('Warning #3 - XMLHTTPRequest connection failed');
}
}
////////////////////////////////////////////////////////////
// This is the callback function, that is called when the "send"
returns
///////////////////////////////////////////////////////////
function callback(){
if(conn.readyState==4){
if(conn.status==200){
result=conn.responseText;
document.body.innerHTML=result;
alert('End');
conn = null;
}
else{
alert('There was a problem with the request.');
}
}
}

Thanks again! <THREAD CLOSED....but more discussion is welcome>

Nov 28 '05 #20

P: n/a
On 2005-11-28, ch***********@gmail.com <ch***********@gmail.com> wrote:
BUT.....if it works as a javascript embeded on the webpage....it should
also work as a bookmarklet too...right? My problem is that if I have a
bookmarklet encoded in a webpage, and execute it from that webpage, IT
WORKS. But if I try to install that same bookmarklet into my browser's
"favorites" and execute it from there (which is the normal procedure
for bookmarklets), it DOES NOT WORK!


what domain does a bookmarklet have?

--

Bye.
Jasen
Dec 1 '05 #21

This discussion thread is closed

Replies have been disabled for this discussion.