Click to See Complete Forum and Search --> : Simple .js question


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:

Brandoe85
12-02-2004, 12:46 AM
How are you calling the .js file when you take out the code?

JavaIlliterate
12-02-2004, 01:29 AM
In the script tag: <SCRIPT LANGUAGE="JavaScript" SRC="mylibary.js">
Is just supposed to enter a number and get the square. Half of what the original program did. I can't even get the prompt or nothing. Have a felling I need more code in the body or something?? no clue.

Brandoe85
12-02-2004, 01:42 AM
Weird...I did that it worked..heres what i did:
Html file:

<html>
<head>
<title>functions</title>
<br />
<img src="fireworks.jpg" name="Fireworks">
<br />
<img src="beach.jpg" name="Beach">
</head>
<body>
<script language = "JavaScript"
type = "text/javascript" src="mylibary.js"><!--
// -->
</script>
</body>
</html>

The JS file, mylibary.js:

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")

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;
}

JavaIlliterate
12-02-2004, 02:40 AM
Thanx. I tried using exactly what you had too and I get nothing but the pics..grrrr. Wonder what I'm doing wrong. If the program works outside of the .js file I am baffled why it won't work outside. With all the examples I've found it says I am doing right. Humph!

7stud
12-02-2004, 07:56 AM
Why do you have <img>'s in the <head> tags? They do not belong there, and having them there has no affect on their positioning on the page.
In the script tag: <SCRIPT LANGUAGE="JavaScript" SRC="mylibary.js">
With all the examples I've found it says I am doing right.
Try this:

<SCRIPT LANGUAGE="JavaScript" SRC="a_test.js"></script>

In IE6, it doesn't work for me without the </script>. In addition, all script tags are supposed to have a 'type' attribute:

http://www.w3.org/TR/html4/interact/scripts.html#edef-SCRIPT

<SCRIPT type="text/javascript" LANGUAGE="JavaScript" SRC="a_test.js"></script>

even though it seems to work without it.

Charles
12-02-2004, 09:12 AM
Originally posted by JavaIlliterate
What am I doing wrong? You are also mixing HTML with XHTML. Get rid of those trailling slashes in the tags for empty elements.