Can I assign an external value to a JS variable?

Discussion in 'Javascript' started by Harry Stangel, Jun 6, 2004.

  1. Noble reader,

    My goal is to obtain the current time on the web server and display it in
    the client's browser. I want the server time displayed independent of the
    client's current clock setting, but use the client clock to refresh the
    displayed time every second. To compensate for drift, I want to
    periodically calibrate the page with the server time by refreshing the page,
    say every ten minutes or so.

    I have a servlet '/apps/clock' that returns the current (server) time as
    XML, i.e.

    <milliseconds>1086478233003</milliseconds>

    I can strip away the XML tags with XSLT, leaving the value '1086478233003'.

    I need a way to read the value into a JavaScript variable, so I can use it
    to create a Date object, increment the variable, etc.

    So, does anyone know a way to assign an external value to a JS variable?

    Best,
    Harry
    Harry Stangel, Jun 6, 2004
    #1
    1. Advertising

  2. Harry Stangel

    Ray Morgan Guest

    "Harry Stangel" <> wrote:
    >I have a servlet '/apps/clock' that returns the current (server) time as
    >XML, i.e.
    >
    ><milliseconds>1086478233003</milliseconds>
    >
    >I can strip away the XML tags with XSLT, leaving the value '1086478233003'.


    Have the servlet do the stripping server-side, and output the
    JavaScript you need. For example, in PHP, it could look something
    like:

    <?php
    # Assume $mil is set to the time in milliseconds via
    # microtime() or something similar
    print "<script type='text/javascript'>\n";
    print "var d = new Date($mil);\n";
    print "</script>\n";
    ?>

    When the HTML page loads, you'll have var d set to the value of the
    server time.

    --
    Ray Morgan
    http://Fares-Fair.com/
    Ray Morgan, Jun 6, 2004
    #2
    1. Advertising

  3. Okay, I know better than to answer my own post, but I discovered this
    method:

    var request;
    var xml;
    var milliseconds;

    // Mozilla technique
    if (window.XMLHttpRequest) {
    request = new XMLHttpRequest();
    request.open("POST", TIMEHOST_URL, false); // we POST to ensure
    the
    // response is not
    cached
    request.send(null);
    xml = request.responseXML;
    if (xml) {
    milliseconds =
    Number(xml.getElementsByTagName("milliseconds").item(0).textContent);
    }
    }
    // IE/ActiveX technique
    else if (window.ActiveXObject) {
    request = new ActiveXObject("Microsoft.XMLHTTP");
    if (request) {
    request.open("POST", TIMEHOST, false);
    request.send();
    xml = request.responseXML;
    if (xml) {
    milliseconds =
    Number(xml.getElementsByTagName("milliseconds").item(0).text);
    }
    }
    }

    Peace,
    Harry

    "Harry Stangel" <> wrote in message
    news:%Tswc.65839$...
    > Noble reader,
    >
    > My goal is to obtain the current time on the web server and display it in
    > the client's browser. I want the server time displayed independent of the
    > client's current clock setting, but use the client clock to refresh the
    > displayed time every second. To compensate for drift, I want to
    > periodically calibrate the page with the server time by refreshing the

    page,
    > say every ten minutes or so.
    >
    > I have a servlet '/apps/clock' that returns the current (server) time as
    > XML, i.e.
    >
    > <milliseconds>1086478233003</milliseconds>
    >
    > I can strip away the XML tags with XSLT, leaving the value

    '1086478233003'.
    >
    > I need a way to read the value into a JavaScript variable, so I can use it
    > to create a Date object, increment the variable, etc.
    >
    > So, does anyone know a way to assign an external value to a JS variable?
    >
    > Best,
    > Harry
    >
    >
    >
    Harry Stangel, Jun 8, 2004
    #3
    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. Matt
    Replies:
    8
    Views:
    53,519
    saravanavsk
    Jan 25, 2010
  2. baumann@pan
    Replies:
    8
    Views:
    744
    Achintya
    May 10, 2005
  3. Nmishra
    Replies:
    3
    Views:
    358
    Default User
    May 24, 2004
  4. Replies:
    18
    Views:
    7,042
    Karl Heinz Buchegger
    Jul 22, 2005
  5. Matt
    Replies:
    2
    Views:
    828
    Anthony Judd
    Sep 25, 2004
Loading...

Share This Page