0

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

4
Contributors
9
Replies
10
Views
5 Years
Discussion Span
Last Post by Troy III
0
<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>
0
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>
0
<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.

0

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 ...

0

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

0

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.

0

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.

Edited by Troy III: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.