Simple Ajax question.

Discussion in 'Javascript' started by Mike Duffy, Dec 23, 2009.

  1. Mike Duffy

    Mike Duffy Guest

    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>
     
    Mike Duffy, Dec 23, 2009
    #1
    1. Advertising

  2. Mike Duffy

    David Mark Guest

    On Dec 22, 9:34 pm, Mike Duffy <> wrote:
    > 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.

    >   };
    >
    >  }
    >  xmlhttp.send(null);}
    >
    > </script>
    >
    > </body>
    > </html>
     
    David Mark, Dec 23, 2009
    #2
    1. Advertising

  3. Mike Duffy

    Mike Duffy Guest

    David Mark <> wrote in
    news::

    > 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>
     
    Mike Duffy, Dec 23, 2009
    #3
  4. Mike Duffy

    David Mark Guest

    On Dec 22, 11:57 pm, Mike Duffy <> wrote:
    > David Mark <> wrote innews::
    >
    > > 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'))
    >   };


    Always set onreadystatechange before opening.
     
    David Mark, Dec 23, 2009
    #4
  5. David Mark wrote:

    > On Dec 22, 11:57 pm, Mike Duffy <> wrote:
    >> 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.


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


    PointedEars
    --
    Anyone who slaps a 'this page is best viewed with Browser X' label on
    a Web page appears to be yearning for the bad old days, before the Web,
    when you had very little chance of reading a document written on another
    computer, another word processor, or another network. -- Tim Berners-Lee
     
    Thomas 'PointedEars' Lahn, Dec 24, 2009
    #5
  6. Mike Duffy

    David Mark Guest

    On Dec 23, 7:40 pm, Thomas 'PointedEars' Lahn <>
    wrote:
    > David Mark wrote:
    > > On Dec 22, 11:57 pm, Mike Duffy <> wrote:
    > >> 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.

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


    Yes, I had that backwards. Thanks.
     
    David Mark, Dec 24, 2009
    #6
  7. Mike Duffy

    Mike Duffy Guest

    Thomas 'PointedEars' Lahn <> wrote in
    news::

    > David Mark wrote:
    >
    >> On Dec 22, 11:57 pm, Mike Duffy <> wrote:
    >>> 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.

    >
    > 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.
     
    Mike Duffy, Dec 24, 2009
    #7
  8. Mike Duffy

    David Mark Guest

    On Dec 23, 8:25 pm, Mike Duffy <> wrote:
    > Thomas 'PointedEars' Lahn <> wrote innews::
    >
    >
    >
    > > David Mark wrote:

    >
    > >> On Dec 22, 11:57 pm, Mike Duffy <> wrote:
    > >>> 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.

    >
    > > 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!)


    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.
     
    David Mark, Dec 24, 2009
    #8
  9. Mike Duffy

    Mike Duffy Guest

    David Mark <> wrote in
    news::

    > 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.
     
    Mike Duffy, Dec 24, 2009
    #9
  10. Mike Duffy

    Mike Duffy Guest

    David Mark <> wrote in
    news::

    > 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.
     
    Mike Duffy, Dec 24, 2009
    #10
  11. Mike Duffy

    David Mark Guest

    On Dec 23, 11:36 pm, Mike Duffy <> wrote:
    > David Mark <> wrote innews::
    >
    > > 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...)


    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?
     
    David Mark, Dec 24, 2009
    #11
  12. Mike Duffy

    Mike Duffy Guest

    David Mark <> wrote in
    news::

    >
    > 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.
     
    Mike Duffy, Dec 24, 2009
    #12
  13. Mike Duffy

    Mike Duffy Guest

    David Mark <> wrote in
    news::

    >
    > 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.
     
    Mike Duffy, Dec 24, 2009
    #13
  14. Mike Duffy

    Mike Duffy Guest

    David Mark <> wrote in
    news::

    > On Dec 23, 11:36 pm, Mike Duffy <> wrote:
    >> David Mark <> wrote


    >
    > 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?
     
    Mike Duffy, Dec 25, 2009
    #14
  15. In comp.lang.javascript message <Xns9CEC76BECB67Drespondinvalidinvali@85
    ..214.113.135>, Fri, 25 Dec 2009 16:40:24, Mike Duffy
    <> posted:
    >
    >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?

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


    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.

    --
    (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
    Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
    Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.
     
    Dr J R Stockton, Dec 26, 2009
    #15
  16. Mike Duffy

    David Mark Guest

    On Dec 25, 11:40 am, Mike Duffy <> wrote:
    > David Mark <> wrote innews::
    >
    > > On Dec 23, 11:36 pm, Mike Duffy <> wrote:
    > >> David Mark <> wrote

    >
    > > 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.


    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.
     
    David Mark, Dec 26, 2009
    #16
  17. Mike Duffy

    Mike Duffy Guest

    David Mark <> wrote in
    news::

    > 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.
     
    Mike Duffy, Dec 27, 2009
    #17
  18. Mike Duffy

    Mike Duffy Guest

    Dr J R Stockton <> wrote in
    news::

    > How do you get that to work for David and not for me?
    >
    >>http://pages.videotron.com/duffym/


    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!
     
    Mike Duffy, Dec 27, 2009
    #18
  19. Mike Duffy

    rf Guest

    "Mike Duffy" <> wrote in message
    news:Xns9CEC76BECB67Drespondinvalidinvali@85.214.113.135...

    > 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/


    Your time is out by one hour. The southern half of the planet is in daylight
    saving time right now.
     
    rf, Dec 27, 2009
    #19
  20. "rf" <> wrote in message
    news:KEDZm.64980$...
    > 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
    >
    >
     
    Richard Maher, Dec 27, 2009
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dondraper
    Replies:
    2
    Views:
    724
    dondraper
    Jan 27, 2006
  2. minnie
    Replies:
    1
    Views:
    710
    Andrew Thompson
    Dec 13, 2006
  3. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Simple AJAX Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Sep 20, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    607
    =?Utf-8?B?UnlhbiBTbWl0aA==?=
    Sep 20, 2007
  4. Steven

    Very simple AJAX question

    Steven, Nov 19, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    366
    Steven
    Nov 20, 2008
  5. Doug Lerner
    Replies:
    0
    Views:
    86
    Doug Lerner
    Apr 16, 2006
Loading...

Share This Page