Click to See Complete Forum and Search --> : Desparately Need Help!!! :)
jscripts
10-06-2003, 09:00 PM
Hi,
I'm new to this forum and this is my first question and I'm really stumped on it. This page, when opened, is supposed to automatically display the first element (element 0) of the array in an alert box.
When you click OK you see a button that says View Next Tip. When you click that it's supposed to display the next alert box (containing element 1), click ok on that alert and if you click the View Next Tip button again it's supposed to display the next tip after that in an alert box (element 2). Once all the tips have been show and you click View Next Tip again it should start at the beginning of the array. (It should start at element 0).
I've got the code printed below and the only part I'm stumped on is what code to put in the function next_alert() that will enable the button to browse through the element of the array. Please can someone help me? :)
<html>
<head>
<SCRIPT language="JavaScript">
<!--HIDE FROM INCOMPATIBLE BROWSERS
var dog_tips = new Array()
dog_tips[0] = "a";
dog_tips[1] = "b";
dog_tips[2] = "c";
dog_tips[3] = "d";
window.alert(dog_tips[0]);
var index = 1;
function next_alert()
{
I DO NOT KNOW WHAT GOES IN HERE
}
//STOP HIDING FROM INCOMPATIBLE BROWSERS-->
</SCRIPT>
</head>
<body>
<form>
<INPUT type="button" value="View Next Tip" onClick=next_alert();>
</form>
</body>
</html>
Charles
10-06-2003, 09:19 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<script type="text/javascript">
<!--
Array.prototype.next = function () {if (this.n == undefined || this.n == this.length) this.n = 0; return this[this.n++]}
var quotes = ['I am the LORD your God, who brought you out of the land of Egypt, out of the house of slavery; you shall have no other gods before me.', 'You shall not make for yourself an idol, whether in the form of anything that is in heaven above, or that is on the earth beneath, or that is in the water under the earth. You shall not bow down to them or worship them; for I the LORD your God am a jealous God, punishing children for the iniquity of parents, to the third and the fourth generation of those who reject me, but showing steadfast love to the thousandth generation of those who love me and keep my commandments. ', 'You shall not make wrongful use of the name of the LORD your God, for the LORD will not acquit anyone who misuses his name.', 'Remember the sabbath day, and keep it holy. Six days you shall labor and do all your work. But the seventh day is a sabbath to the LORD your God; you shall not do any work - you, your son or your daughter, your male or female slave, your livestock, or the alien resident in your towns. For in six days the LORD made heaven and earth, the sea, and all that is in them, but rested the seventh day; therefore the LORD blessed the sabbath day and consecrated it.', 'Honor your father and your mother, so that your days may be long in the land that the LORD your God is giving you.', 'You shall not murder.', 'You shall not commit adultery.', 'You shall not steal.', 'You shall not bear false witness against your neighbor.', 'You shall not covet your neighbor’s house; you shall not covet your neighbor’s wife, or male or female slave, or ox, or donkey, or anything that belongs to your neighbor.']
onload = function () {alert(quotes.next())}
document.write('<p><a href="#">Next tip</a></p>');
document.links[document.links.length-1].onclick = function () {window.onload(); return false}
// -->
</script>
jscripts
10-06-2003, 10:14 PM
Ummm...I really don't understand that code and where it is supposed to go into what I've got written. This is what I had under my function at first:
function next_alert()
{
window.alert(dog_tips[index]);
index++
}
The above code browsed through the records fine but when the records were done it would not go back to the beginning.
jscripts
jscripts
10-06-2003, 10:17 PM
Thanks for the reply and I think I'm starting to understand it..I'm just having a bit of trouble fitting it in with the arrays and the way I got my variables named.
jscripts
jscripts
10-06-2003, 10:27 PM
Sorry but I still can't get it to work with my code....can anyone help me???
jscripts
You don't really have to change much...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<script type="text/javascript">
<!--
Array.prototype.next = function () {if (this.n == undefined || this.n == this.length) this.n = 0; return this[this.n++]}
dog_tips = new Array();
dog_tips[0] = "a";
dog_tips[1] = "b";
dog_tips[2] = "c";
dog_tips[3] = "d";
onload = function () {alert(dog_tips.next())}
document.write('<p><a href="#">Next tip</a></p>');
document.links[document.links.length-1].onclick = function () {window.onload(); return false}
// -->
</script>
jscripts
10-06-2003, 10:37 PM
So the words this.n...I keep that...now I got my function named next_alert...would that name go after the function key word in the example that you gave?
Thanks to both of you for the replies...much appreciated :),
jscripts
Just forget about what you were trying to use, and use what Charles posted...
jscripts
10-06-2003, 11:14 PM
I have but it's still not working for me...is there any different code that I need to use because I am using a button. I'm required to use a button so I'm probably thinking that's my problem..the code in there:
<html>
<head>
<SCRIPT language="JavaScript">
<!--HIDE FROM INCOMPATIBLE BROWSERS
Array.prototype.next = function () {if (this.n == undefined || this.n == this.length) this.n = 0; return this[this.n++]}
var dog_tips = new Array()
dog_tips[0] = "a";
dog_tips[1] = "b";
dog_tips[2] = "c";
dog_tips[3] = "d";
onload = function () {window.alert(dog_tips.next())}
//STOP HIDING FROM INCOMPATIBLE BROWSERS-->
</SCRIPT>
</head>
<body>
<form>
<INPUT type="button" value="View Next Tip" onClick = function () {window.onload(); return false}>
</form>
</body>
</html>
jscripts
10-06-2003, 11:23 PM
It works!!! Yay! Thank you sooo much to the both of you for putting up with my confusion...I was confused for a bit (LOL). Just for a point of reference what exactly is the this.n. I've never seen it before and I was wondering if someone could give a short explanation.
Thanks again sooo much,
jscripts
Charles
10-07-2003, 05:11 AM
Originally posted by jscripts
I'm required to use a buttonSo this is homework is it? If you turn it in using my method then your instructor will know immediately that you have cheated.
jscripts
10-07-2003, 10:49 AM
Ok...how would they know that I have cheated and we were told to look at different sites and references if we were stuck. Are you saying if I didn't use the button they would know? I actually got it working with the button..it took a while but it works now :D
And could someone explain the this.n to me please?
jscripts
They would know for various reasons, with the main one being that there is no way that someone learning JavaScript would have written it as Charles did (unless they were already framiliar with a different language, perhaps). Also, I'm guessing that "...we were told to look at different sites and references if we were stuck." didn't mean to just have someone else make it for you... :rolleyes:
jscripts
10-07-2003, 01:00 PM
Well I DO have experience with other programming languages...three in fact! And FYI but I didn't ask someone to make it for me...I had most of the code done on my own, it was just the function part that I was stuck on...which is BTW only part of what I was supposed to have done with the page. Also I still had to modify the code a great deal because the way it was done didn't work with what I had so I didn't use the code that Charles gave word for word! All I needed was a push in the general direction of what I was supposed to use. I didn't use the code word for word!
So is there anyone out there who can please explain to me the this.n???
jscripts
Charles
10-07-2003, 01:44 PM
In my example "Array.next" is a method that is inherited by all Array objects that have not overwritten their copy. At least you can think of it that way. Actually, when you call "Array.next" the particular Array object is checked and if the method isn't found then the prototype method is used. The word "this" is simply a reference to the particular object that is calling the method. By way of illustration try this:
<script type="text/javascript">
<!--
Array.prototype.example = function () {return this.toString()}
alert(['fee', 'fie', 'foe', 'fum'].example())
alert(['thesis', 'antithesis', 'synthesis'].example())
// -->
</script>
jscripts
10-07-2003, 02:04 PM
ohhh...I get it now...thanks a bunch Charles :D
jscripts