Hi, I used the code below for a function that I have to repeat with the same
code. Samething like the stars in gmail for example that with one click I
sign a message, with the second I sign-off the message.
My code work perfect with Firefox but with Mozilla it work only the first
time that I click the command. I hope my question clear. Sorry for bad
English and thanks for your answers. Stefano.
Ho usato il codiceseguente che sotto mozilla funziona bene, sotto IE mi
funziona solo la prima volta, dalla seconda in poi mi ripete la stessa
cosa...cosa devo cambiare?grazie.
var http_request = false;
function makeRequest(url) {
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
http_request.onreadystatechange = alertContents;
http_request.open('GET', url, true);
http_request.send(null);
}
function alertContents() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
alert(http_request.responseText);
} else {
alert('There was a problem with the request.');
}
}
}
Thomas 'PointedEars' Lahn - 27 Feb 2006 12:39 GMT
> [...]
> My code work perfect with Firefox but with Mozilla it work only the first
[quoted text clipped - 11 lines]
> http_request.onreadystatechange = alertContents;
> http_request.open('GET', url, true);
open() resets all event listeners. Switch the order of the last two
statements.
<URL:http://xulplanet.com/references/objref/XMLHttpRequest.html#method_open>
HTH
PointedEars
ste - 27 Feb 2006 16:34 GMT
>> if (window.XMLHttpRequest) { // Mozilla, Safari,...
>> http_request = new XMLHttpRequest();
[quoted text clipped - 6 lines]
> open() resets all event listeners. Switch the order of the last two
> statements.
Sorry but I have the same problem...
Thomas 'PointedEars' Lahn - 28 Feb 2006 00:55 GMT
[Restored attribution]
> "Thomas 'PointedEars' Lahn" [...] wrote [...]
>>> if (window.XMLHttpRequest) { // Mozilla, Safari,...
[quoted text clipped - 9 lines]
>
> Sorry but I have the same problem...
Since you did not post how this code is called, one can only make an
educated guess, if that. Maybe you are using some `href="javascript:'
code. See the FAQ about it:
<URL:http://jibbering.com/faq/>
Please do not remove previous attribution lines when quoting.
PointedEars
ste - 28 Feb 2006 12:28 GMT
> [Restored attribution]
>> "Thomas 'PointedEars' Lahn" [...] wrote [...]
[quoted text clipped - 14 lines]
> educated guess, if that. Maybe you are using some `href="javascript:'
> code. See the FAQ about it:
I called the code with:
echo "<a href=\"#\"
onclick=\"makeRequest('tick.php?tick=prima&id=$result[id]')\"><img
src=\"img/tick.gif\" border=\"0\" alt=\"Add\"></a>";
with php sintax.
Matt Kruse - 28 Feb 2006 13:00 GMT
> echo "<a href=\"#\"
> onclick=\"makeRequest('tick.php?tick=prima&id=$result[id]')\"><img
> src=\"img/tick.gif\" border=\"0\" alt=\"Add\"></a>";
> with php sintax.
When posting to a javascript newsgroup, it's best to post the souce of the
page after it has been delivered to the browser.
Your code above surely results in this:
<a href="#" onclick="makeRequest('tick.php?tick=prima&id=$result[id]')"><img
src="img/tick.gif" border="0" alt="Add"></a>
which would explain the problem. There is no 'return false' in your onClick
handler, so the browser then goes to your href="#".
See http://www.JavascriptToolbox.com/bestpractices/ for information about
correctly triggering javascript actions from anchor tags.

Signature
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
VK - 27 Feb 2006 17:45 GMT
> Hi, I used the code below for a function that I have to repeat with the same
> code. Samething like the stars in gmail for example that with one click I
[quoted text clipped - 6 lines]
> funziona solo la prima volta, dalla seconda in poi mi ripete la stessa
> cosa...cosa devo cambiare?grazie.
Sorry, but your English version was misleading. The googled Italian one
says:
"under IE me works only before the time, from the second one in then me
repeats the same thing"
Now it's clear: you need either disable caching by setting response
header or you need to randomize your URL.
Quick'n'dirty (presuming your URL already contains some search part
like ...?foo=bar):
// this line is added:
url+= '&rnd=' + (new Date()).getTime();
http_request.open('GET', url, true);
ste - 27 Feb 2006 19:50 GMT
> Now it's clear: you need either disable caching by setting response
> header or you need to randomize your URL.
[quoted text clipped - 6 lines]
>
> http_request.open('GET', url, true);
I solved my problem changing GET with POST!
Thomas 'PointedEars' Lahn - 28 Feb 2006 00:58 GMT
> "VK" [...] wrote [...]:
>> Now it's clear: you need either disable caching by setting response
[quoted text clipped - 9 lines]
>
> I solved my problem changing GET with POST!
It only works now (if it really works) because POST requests are not cached
in _your_ environment. Using some kind of cache control as suggested (where
the above suggestion is not the best approach to this) is the reasonable
course of action.
PointedEars