I need help on javascript....

ok this is what I got from w3 school...

<html>
<body>

<script type="text/javascript">
var d = new Date();
var time = d.getHours();

if (time < ) 
  {
  document.write("<b>Good morning</b>");
  }
</script>

<p>This example demonstrates the If statement.</p>
<p>If the time on your browser is less than 10, you will get a "Good morning" greeting.</p>

</body>
</html>

Ok Instead of saying good morning on this page.. I would love to see if the time is more than 10.. the page will be directed to a another specific page .... anyone can help on this ?

Thanks

Dani AI

Generated

A concise expert note tying the thread together for : correctly flagged the missing comparison, and 's redirect idea is the right direction. To make this robust and friendly, avoid using document.write (it can overwrite the page after load), run the check after the DOM is ready (or place scripts at the end of the body), and use a redirect method that matches the desired browser history behavior.

Example (non-destructive, avoids history pollution):

<script>
document.addEventListener('DOMContentLoaded', function () {
  var hour = new Date().getHours(); // 0-23 local hour
  if (hour < 10) {
    // replace current entry so Back does not return here
    window.location.replace('/check/time');
  } else {
    // insert a small message without document.write
    var note = document.createElement('strong');
    note.textContent = 'Good morning';
    document.body.insertBefore(note, document.body.firstChild);
  }
});
</script>

Notes and cautions:

  • getHours() uses the client machine's local clock and returns 0–23. For time-sensitive or security-critical redirects, perform the check server-side (HTTP 3xx response) so it cannot be bypassed by changing the client clock.
  • window.location.replace(url) prevents a back-button loop; location.href = url or location.assign(url) creates a history entry.
  • If a brief message should be visible before redirect, use setTimeout to delay the navigation by a few hundred milliseconds rather than document.write.
  • Keep logic simple and test across different time zones and devices to ensure the behavior matches the intended schedule.

Recommended Answers

All 9 Replies

<html>
<body>
 
<script type="text/javascript">

var d = new Date();
var time = d.getHours(); 
 
if (time == 10 ) 
  {
  document.write("<b>Good morning</b>");
  }
</script>
 
<p>This example demonstrates the If statement.</p>
<p>If the time on your browser is less than 10, you will get a "Good morning" greeting.</p>
 
</body>
</html>
if (time < )

this is ... well, pretty bad.
"if time smaller than ..." smaller than what? you need to actually compare it to something.

just check this code and adjust yours to what you need:

<html>
<body>
 
<script type="text/javascript">
var d = new Date();
var time = d.getHours();

if (time < 12) 
  {
  document.write("<b>Good morning</b>");
  }

if ( time > 12 ){
  if ( time < 17 ){
    document.write("<b>Good afternoon</b>");
  }
}
</script>
 
<p>This example demonstrates the If statement.</p>
<p>If the time on your browser is less than 10, you will get a "Good morning" greeting.</p>
 
</body>
</html>
<html>
<body>
 
<script type="text/javascript">

var d = new Date();
var time = d.getHours(); 
 
if (time == 10 ) 
  {
  document.write("<b>Good morning</b>");
  }
</script>
 
<p>This example demonstrates the If statement.</p>
<p>If the time on your browser is less than 10, you will get a "Good morning" greeting.</p>
 
</body>
</html>

I think you ment to say

if ( time < 10 )

in your code, only if the hour is equal (not less, as asked) good morning will be printed.

sorry.. what i meant... if the time less than 10... it will directly go to specific page.... let say .. if the time less than 10... automatically direct to "ip_address/check/time"

something like that ...

well, then why don't you add a redirect command in the if block, instead of a print?

ok.. this is my first time doing javascript .. how I can do that .. any pointers?

Your question was:
"... if the time is more than 10.. the page will be directed to a another specific page .... anyone can help on this ?"
The answer is

<html>
<body>

<p>This example demonstrates the If statement.</p>
<p>If the time on your browser is less than 10, you will get a "Good morning" greeting.</p>
<p>Otherwise it will navigate you to a different page.</p>

<script type="text/javascript">
var d = new Date();
var time = d.getHours();

if (time > 10 ) 
  {
  location.href = "http://www.yourdomain.com/theOtherPage";
  }
else
  {
   document.write( "<b>Good morning</b>" );
  }
</script>
</body>
</html>

Script element is moved under the content to prevent overwriting your message.

Ok.. Thanks Troy.. This is what I'm looking for.. :)

Nice..,
in case you don't want the whole message, except the "good morning" salutation when time is less than 10, -you can move your script element up again,
and of course: mark the thread solved!

happy learning's.

Be a part of the DaniWeb community

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