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

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.