?
Solved

Reload page with current time as URL parameter

Posted on 2014-09-25
2
Medium Priority
?
185 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 2000 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

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

743 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