Text on an html page:
<html>
<body>
<script language="javascript">
document.write("Hello World!");
</script>
</body>
</html>
Formatted text on an html page:
<html>
<body>
<script language="javascript">
document.write("<h1>Hello World!</h1>");
</script>
</body>
</html>
Text with a variable on an html page:
<html>
<body>
<script language="javascript">
var name = "Sarah";
document.write("Hello "+name);
</script>
</body>
</html>
html>
<body>
<script language="javascript">
var name = "Harry";
document.write(name);
document.write("<h1>"+name+"</h1>");
</script>
<p>This example declares a variable, assigns a value to it, and then displays the variable.</p>
<p>Then the variable is displayed one more time, only this time as a heading.</p>
</body>
</html>
Showing the if statement
<html>
<body>
<script language="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>
<html>
<body>
<script language="javascript">
var d = new Date();
var time = d.getHours();
if (time < 10)
{
document.write("<b>Good morning</b>");
}
else
{
document.write("<b>Good day</b>")
}
</script>
<p>
This example demonstrates the If...Else statement.
</p>
<p>
If the time on your browser is less than 10,
you will get a "Good morning" greeting.
Otherwise you will get a "Good day" greeting.
</p>
</body>
</html>
<html>
<body>
<script language="javascript">
var d = new Date();
var time = d.getHours();
if (time<10)
{
document.write("<b>Good morning</b>");
}
else if (time>=10 & time<16);
{
document.write("<b>Good day</b>");
}
else
{
document.write("<b>Hello World!</b>");
}
</script>
<p>
This example demonstrates the if..else if...else statement.
</p>
<p>
If the time on your browser is less than 10,
you will get a "Good morning" greeting.
If the time on your browser is between 10 and less than 16 (4pm)you will get a "Good day" greeting.
Greater than 16 you will get a Hello World
</p>
</body>
</html>