Simple Ajax question.

M

Mike Duffy

After reading up a bit on Ajax, decided to try out a simple test script.
It works with IE (8), but not FF (3.5.3). I might add support for other
browsers later using the try..catch techniques that are well-documented
elsewhere, but that would be pointless to try when I cannot correctly
write the absolute simplest example I could think of, which was to take
the jibbering example and then change it to point to my own server.

In effect, I was looking for a script to tell me the server time, because
their clock is always several minutes slow and I need to know how many
minutes I need to wait after uploading a file before my new documents get
treated as being more recent than the ones in the various server cache
proxies.

When I run it with FF, it does not look like the callback is ever
executed. It just sits there. I have looked at dozens of ajax tutorial
sites, and they all boil down to the following code when you take away
the support for old browsers.

And I am using the index page of my website as the Ajax target, because
if I use a 3rd party site, IE gives me the warning about mixing secure /
insecure items.

<html lang="en">
<head>
<title>Test Server Time</title>

</head>

<body onLoad="test()">

<script type="text/javascript">
function test() {
var xmlhttp = new XMLHttpRequest();

xmlhttp.open("HEAD", "http://pages.videotron.com/duffym/index.htm",
false);

xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
document.write('Time at server: ' + xmlhttp.getResponseHeader('Date'))
};

}
xmlhttp.send(null);
}
</script>

</body>
</html>
 
D

David Mark

After reading up a bit on Ajax, decided to try out a simple test script.
It works with IE (8), but not FF (3.5.3). I might add support for other
browsers later using the try..catch techniques that are well-documented
elsewhere, but that would be pointless to try when I cannot correctly
write the absolute simplest example I could think of, which was to take
the jibbering example and then change it to point to my own server.

In effect, I was looking for a script to tell me the server time, because
their clock is always several minutes slow and I need to know how many
minutes I need to wait after uploading a file before my new documents get
treated as being more recent than the ones in the various server cache
proxies.

When I run it with FF, it does not look like the callback is ever
executed. It just sits there. I have looked at dozens of ajax tutorial
sites, and they all boil down to the following code when you take away
the support for old browsers.

And I am using the index page of my website as the Ajax target, because
if I use a 3rd party site, IE gives me the warning about mixing secure /
insecure items.

<html lang="en">
<head>
<title>Test Server Time</title>

</head>

<body onLoad="test()">

<script type="text/javascript">
function test() {
 var xmlhttp = new XMLHttpRequest();

 xmlhttp.open("HEAD", "http://pages.videotron.com/duffym/index.htm",
false);

Synchronous requests are always a bad idea.

https://developer.mozilla.org/en/XmlHttpRequest#open()
 xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) {
   document.write('Time at server: ' + xmlhttp.getResponseHeader('Date'))

Calling document.write on load is also a bad idea as it will wipe out
the previously loaded document.
 
M

Mike Duffy

Synchronous requests are always a bad idea.

I tried it both ways; the result is the same. I presumed sync would be
less trouble.
Calling document.write on load is also a bad idea as it will wipe out
the previously loaded document.

That's a good point. I was doing it just to avoid the neccessity of
constantly dismissing alert boxes while I was trying out various things.
I have replaced it with a textbox and switched to using asynchronous
calls to be more in line with the common usage of the XMLHttpRequest
object. It still works okay with IE8, but not with FF 3.5.3.

Here is the new code; it can be reached on-line at

http://pages.videotron.com/duffym/t.htm

<html lang="en">
<head>
<title>Test Server Time</title>

</head>

<body onLoad="test()">

<script type="text/javascript">
function logMessage(message) {
var dtext = document.forms.debug.debugtext.value;
document.forms.debug.debugtext.value = dtext + message + '\n';
return;
}

function test() {
var xmlhttp = new XMLHttpRequest();

// true = async; false = sync
xmlhttp.open("HEAD", "http://pages.videotron.com/duffym/index.htm",
true);

xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
logMessage("Time at server: " + xmlhttp.getResponseHeader('Date'))
};

}
xmlhttp.send(null);
}
</script>

<p id="debug"><form name="debug">
<textarea name="debugtext" title="Debug" rows="10" cols="50" wrap="true">
</textarea></form></p>


</body>
</html>
 
D

David Mark

I tried it both ways; the result is the same. I presumed sync would be
less trouble.


That's a good point. I was doing it just to avoid the neccessity of
constantly dismissing alert boxes while I was trying out various things.
I have replaced it with a textbox and switched to using asynchronous
calls to be more in line with the common usage of the XMLHttpRequest
object. It still works okay with IE8, but not with FF 3.5.3.

Here is the new code; it can be reached on-line at

http://pages.videotron.com/duffym/t.htm

<html lang="en">
<head>
<title>Test Server Time</title>

</head>

<body onLoad="test()">

<script type="text/javascript">
function logMessage(message) {
var dtext = document.forms.debug.debugtext.value;
document.forms.debug.debugtext.value = dtext + message + '\n';
return;

}

function test() {
 var xmlhttp = new XMLHttpRequest();

 // true = async; false = sync
 xmlhttp.open("HEAD", "http://pages.videotron.com/duffym/index.htm",
true);

 xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) {
   logMessage("Time at server: " + xmlhttp.getResponseHeader('Date'))
  };

Always set onreadystatechange before opening.
 
T

Thomas 'PointedEars' Lahn

David said:
Always set onreadystatechange before opening.

Nonsense. For compatibility, it needs to come afterwards. At least older
Geckos reset the property value on open().


PointedEars
 
D

David Mark

Nonsense.  For compatibility, it needs to come afterwards.  At least older
Geckos reset the property value on open().

Yes, I had that backwards. Thanks.
 
M

Mike Duffy

Nonsense. For compatibility, it needs to come afterwards. At least
older Geckos reset the property value on open().


PointedEars

Changing the order as he suggested did fix my problem. (Thank you David!)

It did not, however seem to work perfectly. Each "refresh" in IE added a
line with the new updated time. However, in FF the identical time was
appended with each refresh.

I fixed that by appending:

"?en&rt=" + Math.random()

to the url. Out of curiousity, I wonder if the problem had anything to do
with the fact that my WSP always run the clock a few minutes slow.

In any case, at least I have a "working" function I can build on.
 
D

David Mark

Changing the order as he suggested did fix my problem. (Thank you David!)

I told you _wrong_ (it must have been a long day). Put the order back
the way it was.

Something else must be wrong and I don't see what it is offhand. Your
code works in the Firebug console, except that logMessage bombs
looking for your form.
 
M

Mike Duffy

Something else must be wrong and I don't see what it is offhand. Your
code works in the Firebug console, except that logMessage bombs
looking for your form.

? I do not doubt you, but that was not my problem, FF just never did
anything. Actually, it did log the message one time but would never do it
again. The error console remained empty. On another occaision, I did see
error text appear in the console spontaneously a few minutes later. I was
trying various things, and as you may gather sometimes get mixed up about
what I have put in the file or javascript library. This is one of those
things I cannot debug offline because of the protocol / domain traversal
problem. (i.e file://C:/My Documents/... vs. http://videotron...)

Also, it might have something to do with the server time being so retarded.
 
M

Mike Duffy

Something else must be wrong and I don't see what it is offhand. Your
code works in the Firebug console, except that logMessage bombs
looking for your form.

? I do not doubt you, but that was not my problem, FF just never did
anything. Actually, it did log the message one time but would never do it
again. The error console remained empty. On another occaision, I did see
error text appear in the console spontaneously a few minutes later. I was
trying various things, and as you may gather sometimes get mixed up about
what I have put in the file or javascript library. This is one of those
things I cannot debug offline because of the protocol / domain traversal
problem. (i.e file://C:/My Documents/... vs. http://videotron...)

Also, it might have something to do with the server time being so retarded.
 
D

David Mark

? I do not doubt you, but that was not my problem, FF just never did
anything. Actually, it did log the message one time but would never do it
again. The error console remained empty. On another occaision, I did see
error text appear in the console spontaneously a few minutes later. I was
trying various things, and as you may gather sometimes get mixed up about
what I have put in the file or javascript library. This is one of those
things I cannot debug offline because of the protocol / domain traversal
problem. (i.e file://C:/My Documents/... vs.http://videotron...)

In FF go to about:config and change
security.fileuri.strict_origin_policy to false.
Also, it might have something to do with the server time being so retarded.

What sort of time is retarded?
 
M

Mike Duffy

What sort of time is retarded?

The server I was tring to get the time from (my web service provider
"videotron") always has their clock set several minutes in the past.

I wanted to display exacty how much retarded it is, because that is how
long I need to wait after uploading a new document before it will pass
through web cache proxy servers. These are used, for example, by Google as
the source for a web page when you request a translation via url.
 
M

Mike Duffy

What sort of time is retarded?

The server I was tring to get the time from (my web service provider
"videotron") always has their clock set several minutes in the past.

I wanted to display exacty how much retarded it is, because that is how
long I need to wait after uploading a new document before it will pass
through web cache proxy servers. These are used, for example, by Google as
the source for a web page when you request a translation via url.
 
M

Mike Duffy

In FF go to about:config and change
security.fileuri.strict_origin_policy to false.

1) My apologies to all for the recent double-posting of my messages. AIEO
via Xnews was showing "Done. Waiting for confirmation." indefinitely.
Without the "Confirmation" I cannot resist the compulsion to post again,
despite the fact that it will usually end up making me look compulsive.

2) Thanks again David. As you may see, I have successfully added clocks to
all my web pages to show server time and local time.

http://pages.videotron.com/duffym/

3) Re your point above about security policies, thanks, I might use this on
a private area of my web site to get the correct time from a government
time server instead of from the local machine. But since this configuration
is not the installed default, I cannot assume that web visitors will have
this option (or the similar IE option) enabled.

But that brings up another question: How does Google implement an Ajax API
(such as the translation one I use extensively) to work without the warning
messages? They "google.com" are, in fact, receiving XML HTTP requests from
everyone all over the place. Most of these users use default security
settings. They are not bothered by those security warning messages.

The google code is obfuscted, I do not need to know THAT much how they do
it. I looked up (on the web) how to do this sort of thing in general, and
there are workarounds, but they all involve setting up a proxy on the end-
user's ISP system.

Do any of you know how they do it?
 
D

Dr J R Stockton

In comp.lang.javascript message <Xns9CEC76BECB67Drespondinvalidinvali@85
..214.113.135>, Fri, 25 Dec 2009 16:40:24, Mike Duffy
2) Thanks again David. As you may see, I have successfully added clocks to
all my web pages to show server time and local time.

How do you get that to work for David and not for me?

Putting the US flag first and the others in what looks like alphabetical
order of English form of language name is politically incorrect. The
flags should be in alphabetical order of the language currently being
shown, and the correct flag for English is of course the Cross of St
George. Latin & Esperanto seem to be missing.


The astro page has NaNs.

The puns page does not translate well.
 
D

David Mark

1) My apologies to all for the recent double-posting of my messages. AIEO
via Xnews was showing "Done. Waiting for confirmation." indefinitely.
Without the "Confirmation" I cannot resist the compulsion to post again,
despite the fact that it will usually end up making me look compulsive.
NP.


2) Thanks again David. As you may see, I have successfully added clocks to
all my web pages to show server time and local time.

http://pages.videotron.com/duffym/

I'll check it out when I get a chance...
3) Re your point above about security policies, thanks, I might use this on
a private area of my web site to get the correct time from a government
time server instead of from the local machine. But since this configuration
is not the installed default, I cannot assume that web visitors will have
this option (or the similar IE option) enabled.

The suggestion was just for local testing.
But that brings up another question: How does Google implement an Ajax API
(such as the translation one I use extensively) to work without the warning
messages? They "google.com" are, in fact, receiving XML HTTP requests from
everyone all over the place. Most of these users use default security
settings. They are not bothered by those security warning messages.

I'll have to look at it.
The google code is obfuscted, I do not need to know THAT much how they do
it. I looked up (on the web) how to do this sort of thing in general, and
there are workarounds, but they all involve setting up a proxy on the end-
user's ISP system.

Do any of you know how they do it?

Not sure until I look at it.
 
M

Mike Duffy

Not sure until I look at it.

If you're curious. But like I said, it is not important for me. I have what
I want. They *might* be using the 1x1 invisible iframe trick to pass data
across domains. Thanks again.
 
M

Mike Duffy

How do you get that to work for David and not for me?

Are you saying that the clocks (local & server) do not work for you? I
have not yet added the support for old (IE6 & <) browsers. Which browser
are you using?

Putting the US flag first and the others in what looks like
alphabetical order of English form of language name is politically
incorrect.

They are in alphabetic order of the drop-down list on the Google
"translate text" page, which is, I believe, exactly as you say it looks
like. English has been pushed to the start in order to reduce the
complexity of debugging.

The flags should be in alphabetical order of the language
currently being shown,

Probably if I were hosting the UN web site I would do as you say, but
that seems to me to add complexity that does really add value to the
product worthy of the effort nor the risk of unforseen complications. To
be fair, I suppose I should use the alphabetic order of the 2 char iso
language code, and put English (en) in it's proper place between Greek
(el) and Spanish (es). The next time I find myself working on that part
of my javascript include file I will do exactly that.

... the correct flag for English is of course
the Cross of St George.

I did actually consider this. But many international users would not make
the connection. As far as that goes, the UK flag is more widely used on
the web to denote the English language. But since my web-site is written
in Quebec English, it makes more sense to use the American flag.

There is a certain body of opinion that holds that flags should not be
used at all for language selection. The problem of choosing which country
flag to use for Swahili was reduced (for me) to picking the country with
the largest number of Swahili speakers. The Indian flag is an obvious
choice for Hindi written in the Devanagari script, but what will I do
when other uniquely Indian languages become supported? I used the Chinese
flag twice, but I cannot forsee doing this for all 400 Indian languages.
And did you recognize the flag I used for Yiddish?

Latin & Esperanto seem to be missing.
As soon as Google supports them, I will add them. And I cannot for the
life of me guess what flags I will use.

The astro page has NaNs.
In the planetary positions, or in the tide (gravity gradient)
calculation? Everything has always worked okay for me. I would appreciate
details on the errors you encountered.

The puns page does not translate well.

If you thinks that's bad, check out my poetry!
 
R

Richard Maher

rf said:
The southern half of the planet is in daylight
saving time right now.

Oh no we're not! At least those of us not living in sunshine-challenged
States anyway :)

Cheers Richard Maher
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top