Hi, I am developing a Java SE console application that needs to connect to a web page, get its contents (using HTTP GET) and get the value of a timer that was written in Javascript.

If we take this example page for instance.

When I retrieve its contents using HTTP GET, I end up with the following code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<title>JavaScript Clock</title>


  JavaScript Clock example

  Copyright Elated Communications Ltd 2007



<style type="text/css">
#clock { font-family: Arial, Helvetica, sans-serif; font-size: 0.8em; color: white; background-color: black; border: 2px solid purple; padding: 4px; }

<script type="text/javascript">

function init ( )
  timeDisplay = document.createTextNode ( "" );
  document.getElementById("clock").appendChild ( timeDisplay );

function updateClock ( )
  var currentTime = new Date ( );

  var currentHours = currentTime.getHours ( );
  var currentMinutes = currentTime.getMinutes ( );
  var currentSeconds = currentTime.getSeconds ( );

  // Pad the minutes and seconds with leading zeros, if required
  currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
  currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

  // Choose either "AM" or "PM" as appropriate
  var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

  // Convert the hours component to 12-hour format if needed
  currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

  // Convert an hours component of "0" to "12"
  currentHours = ( currentHours == 0 ) ? 12 : currentHours;

  // Compose the string for display
  var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;

  // Update the time display
  document.getElementById("clock").firstChild.nodeValue = currentTimeString;

// -->

<body onload="updateClock(); setInterval('updateClock()', 1000 )">

<div style="clear: both;"> </div>

<h1>The JavaScript clock in action</h1>

<p>View the source of this page to see how it works. Feel free to use the JavaScript in your own Web pages!</p>

<div style="width: 10em; text-align: center; margin: 20px auto;">
  <span id="clock">&nbsp;</span>

<p><a href="/articles/creating-a-javascript-clock/">Return to the article</a></p>

<p><small>All code in this page is copyright 2007 <a href="http://www.elated.com">Elated Communicatons Ltd</a></small></p>


As far as I can tell the value of the clock (21:16:37 for example) is in this section: <span id="clock">&nbsp;</span>. How can I get the value of this field after the execution of the Javascript, in other words, how can I get the actual value of the clock (21:16:37 in the example above) instead of the &nbsp; tag?


Recommended Answers

All 3 Replies

Would the time displayed by the JavaScript code (via new Date()) be different from the time the java program would get from the system using some class?

Would the time displayed by the JavaScript code (via new Date()) be different from the time the java program would get from the system using some class?

Yes, the page I sent you was just an example, the actual program should read a countdown timer on a web page.


Why? The time the JS gets is from the same source the java program would get it from.
The time displayed was the current time on the system. It should be the same value if the JS is run in a browser as the java program would get running on the same system.

Why do you think the time the java program would get would be different from the time the JS script would get?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.