473,388 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Can I assign an external value to a JS variable?

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

Jul 23 '05 #1
2 2110
"Harry Stangel" <hs******@pacbell.net> 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/
Jul 23 '05 #2
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" <hs******@pacbell.net> wrote in message
news:%T*******************@newssvr29.news.prodigy. com...
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

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Mike | last post by:
Is there any way I can use a function to create a variable and assign a value to it? I have a Perl script that returns some LDAP information: sn=Shore givenname=Mike logintime=20041008153445Z...
25
by: Rim | last post by:
Hi, I have been thinking about how to overload the assign operation '='. In many cases, I wanted to provide users of my packages a natural interface to the extended built-in types I created for...
2
by: Matt | last post by:
If I assign VBScript server side variable a to javascript variable x, it is fine. <% Dim a, b a = 10 %> var x = <%= a %>; alert(x); But if I do the other way around, then it has 500 error....
4
by: Terry | last post by:
I have a number of input boxes used to display totals based on selected items for each row in a table. There are more than a few rows that are identical, except for the form field name. I have...
4
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; ...
6
by: Stephen Cook | last post by:
Having worked through the problems around enabling the document function using an XmlUrlResolver I started work on building a useful class to hide the intricacies. Trying to generalise the process...
6
by: david | last post by:
I try to use "for" loop to retrieve and assign values in web form. The code is in the following. But it can not be compiled. What I want to do is: txtQ1.Text =...
2
by: Birky | last post by:
Assign the output of a select to a variable? I am unable to find a way to assign the results of an SQL statement to a variable. I know how to assign the SQL statement to a variable but again no...
0
by: GaryGen | last post by:
Hi All, I have a VB.NET program with a COM object associated with an external (legacy) application. If the external application is forced closed, my VB.NET program needs to re-open it. This...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.