Solved

Reload page with current time as URL parameter

Posted on 2014-09-25
2
170 Views
Last Modified: 2014-09-25
I wrote two functions in javascript. The first function displays the current time on the screen. The second takes that time and reloads the page after 5 seconds sending the current value as a URL parameter. Except once I put in the variable the .window.location stops working.

I am not very experienced with javascript and can not seem to figure what I am doing wrong. Here is the code:

<!DOCTYPE html> 
<html> 
<head> 
<script> 
function currentTime() { 
var today=new Date(); 
var Y=today.getFullYear(); 
var M=today.getMonth(); 
var D=today.getDate(); 
var h=today.getHours(); 
var m=today.getMinutes(); 
var s=today.getSeconds(); 
m = checkTime(m); 
s = checkTime(s); 

document.getElementById('clock').innerHTML = Y+ "-"+M+ "-"+D+ " "+h+":"+m+":"+s; 
var t = setTimeout(function(){currentTime()},500); 
} 

function checkTime(i) { 
if (i<10) {i = "0" + i}; // add zero in front of numbers < 10 
return i; 
} 
</script> 

<script> 
<!-- 
function Redirect() 
{ 
window.location.replace = “page.html?var=" Y+ "-"+M+ "-"+D+ " "+h+":"+m+":"+s; 
} 

setTimeout('Redirect()', 5000); 
// 
</script> 
</head> 


<body onload="currentTime()"> 
<div id="clock"></div> 
</body> 
</html> 

Open in new window


Any help would be appreciated.
0
Comment
Question by:serviceco
2 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40344300
<!DOCTYPE html> 
 <html> 
 <head> 
 <script> 

 var today,Y,M,D,h,m,s; 

 function currentTime() { 
 today=new Date(); 
 Y=today.getFullYear(); 
 M=today.getMonth(); 
 D=today.getDate(); 
 h=today.getHours(); 
 m=today.getMinutes(); 
 s=today.getSeconds(); 
 m = checkTime(m); 
 s = checkTime(s); 
 document.getElementById('clock').innerHTML = Y+ "-"+M+ "-"+D+ " "+h+":"+m+":"+s; 
 var t = setTimeout(function(){currentTime()},500); 
 } 

 function checkTime(i) { 
 if (i<10) {i = "0" + i}; // add zero in front of numbers < 10 
 return i; 
 } 
 </script> 

 <script> 
 <!-- 
function Redirect() 
 {
 window.location.replace("page.html?var="+ Y + "-"+M+ "-"+D+ " "+h+":"+m+":"+s); 
 } 

 setTimeout('Redirect()', 5000); 
 // 
 </script> 
 </head> 


 <body onload="currentTime()"> 
 <div id="clock"></div> 
 </body> 
 </html> 

Open in new window

0
 

Author Closing Comment

by:serviceco
ID: 40344430
Thank you!  I understand what I missed. I appreciate the help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now