Busby SEO Test | Loans | Just Holden Commodores | Credit Check | Mortgage Calculator
Javascript Question [Archive] - ZGeek

PDA

View Full Version : Javascript Question


FatherShark
16-08-2005, 06:31 PM
Hi guys, I am doing Web Scripting for a course at college in Canberra Australia. Our first assignment is coming up, and I am struggling. I will attach both the question, and my current solution, and if you could help at all, please reply - thanks:

****if the user clicks on the link to the ‘age.html’ page after 6pm the age page should not be displayed but instead an alert message should be displayed saying “This page is not available until tomorrow due to scheduled server maintenance. You should be doing your homework instead of playing on the computer anyway.”

This is not working for me - the alert does not appear, and the link to age.html is followed regardless of the time of day. Here is my HTML/Javascript:

<html>
<head>
<link rel="stylesheet" href="style.css"/>

<script type="text/javascript">
function agecheck()
{
var current= new Date();
var daynight=current.getHours();
if (daynight<=18)
returnfalse();
alert('This page is not available until tomorrow due to scheduled server maintenance. You should be doing your homework instead of playing on the computer anyway');
else
returntrue();

}
</script>
</head>
<body>

<h1>Justin Newbury</h1>

<h2>Welcome Page</h2>

<script type="text/javascript">
var current= new Date();
var daynight=current.getHours();
if (daynight<=18)
document.write("<img src='images/day.jpg'>");
else
document.write("<img src='images/night.jpg'>");
</script>

<br />
<br />
<a href="photo.html">The Photo Page</a>
<br />
<a href="bit.html">A Bit About Me</a>
<br />
<a href="age.html" onClick="agecheck();">The Age Page</a>
<br />

</body>

</html>

locust
16-08-2005, 07:52 PM
I barely know Javascript, but..

add braces to your if..else block.

Alert before you return.

dwarfthrower
16-08-2005, 08:17 PM
yer calling returnfalse() and returntrue() as functions, but haven't defined them as functions anywhere.

Perhaps you meant;

return true;

and

return false;

and yeah... you can never have too many braces and your alert should come before your return value... return anything exits out of the function. No further code is executed after that point.

... also, the link to age.html will be followed regardless unless you actually return false to the event handler.

<a href="age.html" onClick="return agecheck();">The Age Page</a>

should see a better result.

evil
16-08-2005, 08:19 PM
locust was right, you needed to add braces to the if/then statements.

you needed to change "returnTrue()" and "returnFalse()" to "return true" and "return false". You also needed to move the "return false" to after the alert.

And finally, in the "onClick" bit you needed to add a "return". So it shoud read onClick to onClick="return agecheck();"

As a style pointer, it's best to put your javascript code within commented out sections so old non javascript browsers don't crash. You use "<!--" and "-->".

Here's the working version:


<html>
<head>
<link rel="stylesheet" href="style.css"/>

<script type="text/javascript">
<!--
function agecheck() {
var current= new Date();
var daynight=current.getHours();
if (daynight<=18) {
alert('This page is not available until tomorrow due to scheduled server maintenance. You should be doing your homework instead of playing on the computer anyway');
return false;
} else {
return true;
}
}
-->
</script>
</head>
<body>

<h1>Justin Newbury</h1>

<h2>Welcome Page</h2>

<script type="text/javascript">
<!--
var current= new Date();
var daynight=current.getHours();
if (daynight<=18) {
document.write("<img src='images/day.jpg'>");
} else {
document.write("<img src='images/night.jpg'>");
}
-->
</script>

<br />
<br />
<a href="photo.html">The Photo Page</a>
<br />
<a href="bit.html">A Bit About Me</a>
<br />
<a href="age.html" onClick="return agecheck();">The Age Page</a>
<br />

</body>

</html>

FatherShark
17-08-2005, 10:28 AM
I'd like to thank you for your help.

Although I love HTML and CSS, coming from a non-programming background I am finding Javascript quite a challenge. Is there any on-line resources you might suggest to help bring me up to speed? I'd be satisfied if I can adapt existing code rather than just write it!

dwarfthrower
17-08-2005, 04:52 PM
www.tek-tips.com is a good resource. Been around for years... most javascript issues have been thrashed out there once or twice. You can always post a new question there too and it should get answered pretty smartly... just be careful about the words "school" and "assignment" there.