JavaIlliterate
12-01-2004, 10:49 PM
I (hense the name) am having mega probs with this. All I have to do is transfer my code to .js file. I can't not get it work after I transfer. What am I doing wrong?
Original code:
<html>
<head>
<title>functions</title>
<br />
<img src="fireworks.jpg" name="Fireworks">
<br />
<img src="beach.jpg" name="Beach">
<br /><script language="JavaScript" type="text/javascript"><!--
var num = 0;
//**************************************************************
function getNum (num)
{
num = parseInt (prompt ("Please enter a number:", ""));
while (isNaN (num))
{
alert("Error! Must be a number");
num = parseInt (prompt ("Please enter a number:", ""));
}
return num;
}
//**************************************************************
function calcSquare ()
{
return num * num;
}
//**************************************************************
function writeList (num)
{
document.write("<ol>");
for (i = 1; i <= num; i++)
{
document.write ("<li>Listed Item #" + i + "</li>")
}
document.write("</ol>")
}
//**************************************************************
function swapImg (first, second, status)
{
document.fireworks.src = imgArray[second].src;
window.status=status;
}
//**************************************************************
// -->
</script>
</head>
<body>
<script language = "JavaScript"
type = "text/javascript"><!--
var num = 0;
var imgArray = new Array()
imgArray[0] = new Image()
imgArray[0].src = "fireworks.jpg"
imgArray[1] = new Image()
imgArray[1].src = "beach.jpg"
num = getNum ();
document.write("Result of square function: " + calcSquare ());
writeList (5);
writeList (10);
swapImg ("fireworks", 1, "Swapping the 1st with the 2nd image")
// -->
</script>
</body>
</html>
and I copied the 4 fuctions into a .js file and now when I put the src in the sript tag for .js file nothing happens? I must be foregetting something but I have NOO idea what! Could someone give me an example of what it should look like with the .js extention? and if maybe there is something else I should have in the file that I don't. This is what I have in there so far:
My .js Library
//**************************************************************
function getNum (num)
{
num = parseInt (prompt ("Please enter a number:", ""));
while (isNaN (num))
{
alert("Error! Must be a number");
num = parseInt (prompt ("Please enter a number:", ""));
}
return num;
}
//**************************************************************
function calcSquare ()
{
return num * num;
}
//**************************************************************
function writeList (num)
{
document.write("<ol>");
for (i = 1; i <= num; i++)
{
document.write ("<li>Listed Item #" + i + "</li>")
}
document.write("</ol>")
}
//**************************************************************
function swapImg (first, second, status)
{
document.fireworks.src = imgArray[second].src;
window.status=status;
}
//**************************************************************
I realize my swap image isn't working either, but I'm more worried about what I'm doing wrong with the .js.:confused:
Original code:
<html>
<head>
<title>functions</title>
<br />
<img src="fireworks.jpg" name="Fireworks">
<br />
<img src="beach.jpg" name="Beach">
<br /><script language="JavaScript" type="text/javascript"><!--
var num = 0;
//**************************************************************
function getNum (num)
{
num = parseInt (prompt ("Please enter a number:", ""));
while (isNaN (num))
{
alert("Error! Must be a number");
num = parseInt (prompt ("Please enter a number:", ""));
}
return num;
}
//**************************************************************
function calcSquare ()
{
return num * num;
}
//**************************************************************
function writeList (num)
{
document.write("<ol>");
for (i = 1; i <= num; i++)
{
document.write ("<li>Listed Item #" + i + "</li>")
}
document.write("</ol>")
}
//**************************************************************
function swapImg (first, second, status)
{
document.fireworks.src = imgArray[second].src;
window.status=status;
}
//**************************************************************
// -->
</script>
</head>
<body>
<script language = "JavaScript"
type = "text/javascript"><!--
var num = 0;
var imgArray = new Array()
imgArray[0] = new Image()
imgArray[0].src = "fireworks.jpg"
imgArray[1] = new Image()
imgArray[1].src = "beach.jpg"
num = getNum ();
document.write("Result of square function: " + calcSquare ());
writeList (5);
writeList (10);
swapImg ("fireworks", 1, "Swapping the 1st with the 2nd image")
// -->
</script>
</body>
</html>
and I copied the 4 fuctions into a .js file and now when I put the src in the sript tag for .js file nothing happens? I must be foregetting something but I have NOO idea what! Could someone give me an example of what it should look like with the .js extention? and if maybe there is something else I should have in the file that I don't. This is what I have in there so far:
My .js Library
//**************************************************************
function getNum (num)
{
num = parseInt (prompt ("Please enter a number:", ""));
while (isNaN (num))
{
alert("Error! Must be a number");
num = parseInt (prompt ("Please enter a number:", ""));
}
return num;
}
//**************************************************************
function calcSquare ()
{
return num * num;
}
//**************************************************************
function writeList (num)
{
document.write("<ol>");
for (i = 1; i <= num; i++)
{
document.write ("<li>Listed Item #" + i + "</li>")
}
document.write("</ol>")
}
//**************************************************************
function swapImg (first, second, status)
{
document.fireworks.src = imgArray[second].src;
window.status=status;
}
//**************************************************************
I realize my swap image isn't working either, but I'm more worried about what I'm doing wrong with the .js.:confused: