Solved

Reload page with current time as URL parameter

Posted on 2014-09-25
2
168 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

705 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

16 Experts available now in Live!

Get 1:1 Help Now