Connecting Tech Pros Worldwide Help | Site Map

Can I assign an external value to a JS variable?

  #1  
Old July 23rd, 2005, 12:24 PM
Harry Stangel
Guest
 
Posts: n/a
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



  #2  
Old July 23rd, 2005, 12:25 PM
Ray Morgan
Guest
 
Posts: n/a

re: Can I assign an external value to a JS variable?


"Harry Stangel" <hstangel@pacbell.net> wrote:[color=blue]
>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'.[/color]

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/
  #3  
Old July 23rd, 2005, 12:27 PM
Harry Stangel
Guest
 
Posts: n/a

re: Can I assign an external value to a JS variable?


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").it em(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").it em(0).text);
}
}
}

Peace,
Harry

"Harry Stangel" <hstangel@pacbell.net> wrote in message
news:%Tswc.65839$LO7.45733@newssvr29.news.prodigy. com...[color=blue]
> 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[/color]
page,[color=blue]
> 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[/color]
'1086478233003'.[color=blue]
>
> 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
>
>
>[/color]


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
setting global variables in javascript from html. sabbbih answers 3 September 1st, 2008 09:10 AM
array.["property"] josh answers 30 November 30th, 2006 10:25 AM
adding to a BODY-onload function from within the page laredotornado@zipmail.com answers 2 September 12th, 2005 12:35 AM
Detect element on OnClick event of body? Noozer answers 10 September 7th, 2005 10:05 AM