...

View Full Version : Struggling with arrays



sfraise
03-13-2009, 03:10 AM
Ok, I'm struggling with setting up an array to randomly select what to be shown in a scrolling pop up that looks like an im.

Here's the working script I have now, just without the arrays:


HEADER CODE:
<LINK REL=StyleSheet HREF="scrolling_popup.css" TYPE="text/css">
<script type="text/javascript" language="javascript" src="scrolling_popup.js"></script>
<script type="text/javascript" >

var html_code = '<table border="0" width="100%"<tr><td valign="top"><img src="http://www.oohya.net/im.jpg" width="62" height="77"></td><td valign="top">Hey we live close to each other.<br>U Single?<br><br><a href="http://www.oohya.org" target="_blank">Reply</a></td></tr></table>';

buildPopup_HtmlCode(200, 140, 'IM from: sweetlips69 F/23', html_code);

</script>

And this is the other part of the script that is placed in the footer:


<script type="text/javascript" >
setTimeout("ShowTheBox(false, rightSide, bottomCorner, bottopUp)", 10000);
</script>


Now I've tried several variations using similar code to this:


Header script:
<LINK REL=StyleSheet HREF="scrolling_popup.css" TYPE="text/css">
<script type="text/javascript" language="javascript" src="scrolling_popup.js"></script>
<script type="text/javascript" >

var array=new Array()
array[0]='<table border="0" width="100%"<tr><td valign="top"><img src="http://www.oohya.net/im.jpg" width="62" height="77"></td><td valign="top">Hey we live close to each other.<br>U Single?<br><br><a href="http://www.oohya.org" target="_blank">Reply</a></td></tr></table>';
array[1]='<table border="0" width="100%"<tr><td valign="top"><img src="http://www.oohya.net/im2.jpg" width="62" height="77"></td><td valign="top">Hey I live close to u!<br>R U Single?<br><br><a href="http://www.oohya.org" target="_blank">Reply</a></td></tr></table>';
array[2]='<table border="0" width="100%"<tr><td valign="top"><img src="http://www.oohya.net/im3.jpg" width="62" height="77"></td><td valign="top">U don't live too far from me!<br>Wanna hook up sometime?<br><br><a href="http://www.oohya.org" target="_blank">Reply</a></td></tr></table>';
function switch(num){
var html_code = array[num]
}
Math.random(array.length)
buildPopup_HtmlCode(200, 140, 'IM from: sweetlips69 F/23', html_code);

</script>


Please help

Old Pedant
03-13-2009, 03:24 AM
Ummm....

Where, pray tell, is ShowTheBox( )???

And you can't pass named value to a function invoked via setTimeout unless they are global variables on the page.

And for that matter, where are all those values you are passing to ShowTheBox( ) supposed to be coming from?

And this code makes no sense:


function switch(num){
var html_code = array[num]
}
Math.random(array.length)

(1) switch( ) is a keyword in JavaScript
(2) you are assigning the value of one of the array elements to the variable html_code, but because that variable is defined local to that function, it just gets tossed in the garbage when the function terminates on the next line.
(3) Okay, you are generating some sort of random number. But you don't assign it to anything or do anything with it. Oh...and Math.random() does *NOT* take an argument.

*************

Ignoring the stuff in your footer that doesn't seem at all related to the rest of this, I *think* that all you need is this:



<script type="text/javascript">

array[0]='<table border="0" width="100%"<tr><td valign="top"><img src="http://www.oohya.net/im.jpg" width="62" height="77"></td><td valign="top">Hey we live close to each other.<br>U Single?<br><br><a href="http://www.oohya.org" target="_blank">Reply</a></td></tr></table>';
array[1]='<table border="0" width="100%"<tr><td valign="top"><img src="http://www.oohya.net/im2.jpg" width="62" height="77"></td><td valign="top">Hey I live close to u!<br>R U Single?<br><br><a href="http://www.oohya.org" target="_blank">Reply</a></td></tr></table>';
array[2]='<table border="0" width="100%"<tr><td valign="top"><img src="http://www.oohya.net/im3.jpg" width="62" height="77"></td><td valign="top">U don't live too far from me!<br>Wanna hook up sometime?<br><br><a href="http://www.oohya.org" target="_blank">Reply</a></td></tr></table>';

var html_code = array[ Math.floor( array.length * Math.random() ) ];
buildPopup_HtmlCode(200, 140, 'IM from: sweetlips69 F/23', html_code);

</script>

sfraise
03-13-2009, 03:38 AM
Where, pray tell, is ShowTheBox( )???
Answer: src="scrolling_popup.js"


// float directions
var leftRight = 1;
var rightLeft = 2;
var topDown = 3;
var bottopUp = 4;

// side
var leftSide = 1;
var rightSide = 2;

// position
var topCorner = 1;
var bottomCorner = 2;

// default title
var _title = 'Put here your title';

// default width
var popupWidth = 210;
var popupHeight = 81;

var only_once_per_browser = false;

var ns4 = document.layers;
var ie4 = document.all;
var ns6 = document.getElementById&&!document.all;
var crossobj;

function getCrossObj()
{
var contentDiv;
var titleDiv;

if (ns4)
{
crossobj = document.layers.postit;
contentDiv = document.layers.postit_content;
titleDiv = document.layers.postit_title;
}
else if (ie4||ns6)
{
crossobj = ns6? document.getElementById("postit") : document.all.postit;
contentDiv = ns6? document.getElementById("postit_content") : document.all.postit_content;
titleDiv = ns6? document.getElementById("postit_title") : document.all.postit_title;
}
crossobj.style.width = popupWidth + 'px';
crossobj.style.height = popupHeight + 'px';

// adjust the size of the div "content"
contentDiv.style.width = (popupWidth-8) + 'px';
contentDiv.style.height = (popupHeight-26) + 'px';

// adjust the width of the div "title"
titleDiv.style.width = (popupWidth-8) + 'px';

}

//
// buildPopup_Frame
// passing it the url of the frame to display inside
//
function buildPopup_Frame(width, height, title, framesrc)
{
if (width)
popupWidth = width;

if (height)
popupHeight = height;

if (title)
_title = title


document.write('<div id="postit" class="postit" >');

document.write('<div id="postit_title" class="title"><b>' + _title + ' <span class="spantitle"><img src="close.gif" border="0" title="Close" align="right" WIDTH="11" HEIGHT="11" onclick="closeit()">&nbsp;</b></span></div>');
document.write('<div id="postit_content" class="content">');

document.write('<iframe src="' + framesrc + '" width="100%" height="100%" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no" bordercolor="#000000"></iframe>');

document.write('</div></div>');

getCrossObj();
}

//
// buildPopup_HtmlCode
// build popup passing it the html code to put inside
//
function buildPopup_HtmlCode(width, height, title, htmlCode)
{
if (width)
popupWidth = width;

if (height)
popupHeight = height;

if (title)
_title = title

document.write('<div id="postit" class="postit">');

document.write('<div id="postit_title" class="title"><b>' + _title + ' <span class="spantitle"><img src="close.gif" border="0" title="Close" align="right" WIDTH="11" HEIGHT="11" onclick="closeit()">&nbsp;</b></span></div>');
document.write('<div id="postit_content" class="content">');

document.write(htmlCode);

document.write('</div></div>');

getCrossObj();
}

//
// closeit
//
function closeit()
{
if (ie4||ns6)
crossobj.style.visibility="hidden";
else if (ns4)
crossobj.visibility="hide";
}

//
// get_cookie
//
function get_cookie(Name)
{
var search = Name + "=";
var returnvalue = "";

if (document.cookie.length > 0)
{
offset = document.cookie.indexOf(search);
if (offset != -1)
{
// if cookie exists
offset += search.length;
// set index of beginning of value
end = document.cookie.indexOf(";", offset);
// set index of end of cookie value
if (end == -1)
end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end));
}
}
return returnvalue;
}

//
// check the cookie
//
function showOrNot(direction)
{
var showit = false;

if (get_cookie('postTheBoxDisplay')=='')
{
showit = true;
document.cookie = "postTheBoxDisplay=yes";
}
return showit;
}

//
// showItRight
//
function showIt(direction)
{
var steps;

steps = Math.floor(popupHeight / 4)+5;


if (ie4||ns6)
{
crossobj.style.visibility = "visible";
if ((direction == rightLeft) || (direction == leftRight))
flyTheBox(direction, 0, popupWidth , steps, 1000);
else
flyTheBox(direction, 0, popupHeight , steps, 1000);
}
else if (ns4)
crossobj.visibility = "show";
}

//
// flyTheBox
//
function flyTheBox(direction, start, end, steps, msec, counter)
{
if(!counter)
counter = 1;

var tmp;

if(start < end)
{
if (direction == rightLeft)
crossobj.style.width = end / steps * counter + 'px';
else if (direction == bottopUp)
crossobj.style.height = end / steps * counter + 'px';
else if (direction == topDown)
crossobj.style.top = ((end / steps * counter) - popupHeight) + 'px';
else if (direction == leftRight)
crossobj.style.left = (end / steps * counter)-popupWidth + 'px';

}
else
{

tmp=steps - counter;
if (direction == rightLeft)
crossobj.style.width = start / steps * tmp + 'px';
else if (direction == bottopUp)
crossobj.style.height = start / steps * tmp + 'px';
else if (direction == topDown)
crossobj.style.top = ((end / steps * counter) - popupHeight) + 'px';

}
if(counter != steps)
{
counter++;
flyTheBox_timer=setTimeout('flyTheBox('+ direction + ',' + start + ','+ end + ',' + steps + ',' + msec + ', '+ counter + ')', msec/steps);
}
else
{
if(start > end)
crossobj.style.display = 'none';
}
}


//
// ShowTheBox
//
function ShowTheBox(only_once, side, corner, direction)
{
if (side == leftSide)
{
if (direction == rightLeft)
return;
crossobj.style.left = '1px';
}
else
{
if (direction == leftRight)
return;
crossobj.style.right = '1px';
}

if ((corner == topCorner) && (direction == bottopUp))
return;

if ((corner == bottomCorner) && (direction == topDown))
return;

if ( (direction == topDown) && (corner == topCorner) )
crossobj.style.top = '-' + popupHeight + 'px';
else if ( ((direction == rightLeft)||(direction == leftRight)) && (corner == topCorner) )
crossobj.style.top = '1px';
else if (corner == bottomCorner)
crossobj.style.bottom = '2px';

if (only_once)
only_once_per_browser = only_once;

if (only_once_per_browser)
{
// verify the presence of a cookie
if (showOrNot())
showIt(direction);
}
else
setTimeout("showIt("+ direction + ")",1030);
}

Does that make any more sense?

TinyScript
03-13-2009, 03:48 AM
this will do it I think

function switch(){
var num= Math.random(array.length)
var html_code = array[num]
}


then just call switch

TinyScript
03-13-2009, 03:55 AM
<script type="text/javascript" >

var array=new Array()
array[0]='<table border="0" width="100%"<tr><td valign="top"><img src="http://www.oohya.net/im.jpg" width="62" height="77"></td><td valign="top">Hey we live close to each other.<br>U Single?<br><br><a href="http://www.oohya.org" target="_blank">Reply</a></td></tr></table>';
array[1]='<table border="0" width="100%"<tr><td valign="top"><img src="http://www.oohya.net/im2.jpg" width="62" height="77"></td><td valign="top">Hey I live close to u!<br>R U Single?<br><br><a href="http://www.oohya.org" target="_blank">Reply</a></td></tr></table>';
array[2]='<table border="0" width="100%"<tr><td valign="top"><img src="http://www.oohya.net/im3.jpg" width="62" height="77"></td><td valign="top">U don't live too far from me!<br>Wanna hook up sometime?<br><br><a href="http://www.oohya.org" target="_blank">Reply</a></td></tr></table>';
function switch(){
var num= Math.random(array.length)
var html_code = array[num]
buildPopup_HtmlCode(200, 140, 'IM from: sweetlips69 F/23', html_code);
}



</script>
<body onload="switch()">

sfraise
03-13-2009, 04:11 AM
Thanks for all the help, but still not getting it.
I've tried:


Head:
<LINK REL=StyleSheet HREF="scrolling_popup.css" TYPE="text/css">
<script type="text/javascript" language="javascript" src="scrolling_popup.js"></script>
<script type="text/javascript" >
var array=new Array()
array[0]='<table border="0" width="100%"<tr><td valign="top"><img src="http://www.oohya.net/im.jpg" width="62" height="77"></td><td valign="top">Hey we live close to each other.<br>U Single?<br><br><a href="http://www.oohya.org" target="_blank">Reply</a></td></tr></table>';
array[1]='<table border="0" width="100%"<tr><td valign="top"><img src="http://www.oohya.net/im2.jpg" width="62" height="77"></td><td valign="top">Hey I live close to u!<br>R U Single?<br><br><a href="http://www.oohya.org" target="_blank">Reply</a></td></tr></table>';
array[2]='<table border="0" width="100%"<tr><td valign="top"><img src="http://www.oohya.net/im3.jpg" width="62" height="77"></td><td valign="top">U don't live too far from me!<br>Wanna hook up sometime?<br><br><a href="http://www.oohya.org" target="_blank">Reply</a></td></tr></table>';
function switch(){
var num= Math.random(array.length)
var html_code = array[num]
buildPopup_HtmlCode(200, 140, 'IM from: sweetlips69 F/23', html_code);
}
</script>

Footer:
<script type="text/javascript" >
onLoad="switch()";
setTimeout("ShowTheBox(false, rightSide, bottomCorner, bottopUp)", 30000);
</script>


Still no luck.

TinyScript
03-13-2009, 04:18 AM
no, I'm sorry, it doesn't work. I have no idea why. I've used the exact same function before many many times. There's a problem with the way you're passing the html_code or something. isn't there a working example of the popup you can show us? I did notice you were missing the ends of the table tags and you put a ' in the word don't, which is a problem

sfraise
03-13-2009, 04:29 AM
Yeah, I have this working fine at www.oohya.net, it will scroll up at the bottom right of the page after 30 seconds.
Using the following code:
Header


<LINK REL=StyleSheet HREF="scrolling_popup.css" TYPE="text/css">
<script type="text/javascript" language="javascript" src="scrolling_popup.js"></script>
<script type="text/javascript" >

var html_code = '<table border="0" width="100%"<tr><td valign="top"><img src="http://www.oohya.net/im.jpg" width="62" height="77"></td><td valign="top">Hey we live close to each other.<br>U Single?<br><br><a href="http://www.oohya.org" target="_blank">Reply</a></td></tr></table>';

buildPopup_HtmlCode(200, 140, 'IM from: sweetlips69 F/23', html_code);

</script>

Footer


<script type="text/javascript" >
//ShowTheBox(false, rightSide, bottomCorner, rightLeft);
setTimeout("ShowTheBox(false, rightSide, bottomCorner, bottopUp)", 30000);
//ShowTheBox(false, rightSide, topCorner, topDown);
//ShowTheBox(false, rightSide, topCorner, rightLeft);

//ShowTheBox(false, leftSide, bottomCorner, leftRight);
//ShowTheBox(false, leftSide, bottomCorner, bottopUp);
//ShowTheBox(false, leftSide, topCorner, topDown);
//ShowTheBox(false, leftSide, topCorner, leftRight);
//ShowTheBox(false, leftSide, topCorner, leftRight);
</script>


This of course just uses the one option.

The scrolling_popup.js code is:


// float directions
var leftRight = 1;
var rightLeft = 2;
var topDown = 3;
var bottopUp = 4;

// side
var leftSide = 1;
var rightSide = 2;

// position
var topCorner = 1;
var bottomCorner = 2;

// default title
var _title = 'Put here your title';

// default width
var popupWidth = 210;
var popupHeight = 81;

var only_once_per_browser = false;

var ns4 = document.layers;
var ie4 = document.all;
var ns6 = document.getElementById&&!document.all;
var crossobj;

function getCrossObj()
{
var contentDiv;
var titleDiv;

if (ns4)
{
crossobj = document.layers.postit;
contentDiv = document.layers.postit_content;
titleDiv = document.layers.postit_title;
}
else if (ie4||ns6)
{
crossobj = ns6? document.getElementById("postit") : document.all.postit;
contentDiv = ns6? document.getElementById("postit_content") : document.all.postit_content;
titleDiv = ns6? document.getElementById("postit_title") : document.all.postit_title;
}
crossobj.style.width = popupWidth + 'px';
crossobj.style.height = popupHeight + 'px';

// adjust the size of the div "content"
contentDiv.style.width = (popupWidth-8) + 'px';
contentDiv.style.height = (popupHeight-26) + 'px';

// adjust the width of the div "title"
titleDiv.style.width = (popupWidth-8) + 'px';

}

//
// buildPopup_Frame
// passing it the url of the frame to display inside
//
function buildPopup_Frame(width, height, title, framesrc)
{
if (width)
popupWidth = width;

if (height)
popupHeight = height;

if (title)
_title = title


document.write('<div id="postit" class="postit" >');

document.write('<div id="postit_title" class="title"><b>' + _title + ' <span class="spantitle"><img src="close.gif" border="0" title="Close" align="right" WIDTH="11" HEIGHT="11" onclick="closeit()">&nbsp;</b></span></div>');
document.write('<div id="postit_content" class="content">');

document.write('<iframe src="' + framesrc + '" width="100%" height="100%" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no" bordercolor="#000000"></iframe>');

document.write('</div></div>');

getCrossObj();
}

//
// buildPopup_HtmlCode
// build popup passing it the html code to put inside
//
function buildPopup_HtmlCode(width, height, title, htmlCode)
{
if (width)
popupWidth = width;

if (height)
popupHeight = height;

if (title)
_title = title

document.write('<div id="postit" class="postit">');

document.write('<div id="postit_title" class="title"><b>' + _title + ' <span class="spantitle"><img src="close.gif" border="0" title="Close" align="right" WIDTH="11" HEIGHT="11" onclick="closeit()">&nbsp;</b></span></div>');
document.write('<div id="postit_content" class="content">');

document.write(htmlCode);

document.write('</div></div>');

getCrossObj();
}

//
// closeit
//
function closeit()
{
if (ie4||ns6)
crossobj.style.visibility="hidden";
else if (ns4)
crossobj.visibility="hide";
}

//
// get_cookie
//
function get_cookie(Name)
{
var search = Name + "=";
var returnvalue = "";

if (document.cookie.length > 0)
{
offset = document.cookie.indexOf(search);
if (offset != -1)
{
// if cookie exists
offset += search.length;
// set index of beginning of value
end = document.cookie.indexOf(";", offset);
// set index of end of cookie value
if (end == -1)
end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end));
}
}
return returnvalue;
}

//
// check the cookie
//
function showOrNot(direction)
{
var showit = false;

if (get_cookie('postTheBoxDisplay')=='')
{
showit = true;
document.cookie = "postTheBoxDisplay=yes";
}
return showit;
}

//
// showItRight
//
function showIt(direction)
{
var steps;

steps = Math.floor(popupHeight / 4)+5;


if (ie4||ns6)
{
crossobj.style.visibility = "visible";
if ((direction == rightLeft) || (direction == leftRight))
flyTheBox(direction, 0, popupWidth , steps, 1000);
else
flyTheBox(direction, 0, popupHeight , steps, 1000);
}
else if (ns4)
crossobj.visibility = "show";
}

//
// flyTheBox
//
function flyTheBox(direction, start, end, steps, msec, counter)
{
if(!counter)
counter = 1;

var tmp;

if(start < end)
{
if (direction == rightLeft)
crossobj.style.width = end / steps * counter + 'px';
else if (direction == bottopUp)
crossobj.style.height = end / steps * counter + 'px';
else if (direction == topDown)
crossobj.style.top = ((end / steps * counter) - popupHeight) + 'px';
else if (direction == leftRight)
crossobj.style.left = (end / steps * counter)-popupWidth + 'px';

}
else
{

tmp=steps - counter;
if (direction == rightLeft)
crossobj.style.width = start / steps * tmp + 'px';
else if (direction == bottopUp)
crossobj.style.height = start / steps * tmp + 'px';
else if (direction == topDown)
crossobj.style.top = ((end / steps * counter) - popupHeight) + 'px';

}
if(counter != steps)
{
counter++;
flyTheBox_timer=setTimeout('flyTheBox('+ direction + ',' + start + ','+ end + ',' + steps + ',' + msec + ', '+ counter + ')', msec/steps);
}
else
{
if(start > end)
crossobj.style.display = 'none';
}
}


//
// ShowTheBox
//
function ShowTheBox(only_once, side, corner, direction)
{
if (side == leftSide)
{
if (direction == rightLeft)
return;
crossobj.style.left = '1px';
}
else
{
if (direction == leftRight)
return;
crossobj.style.right = '1px';
}

if ((corner == topCorner) && (direction == bottopUp))
return;

if ((corner == bottomCorner) && (direction == topDown))
return;

if ( (direction == topDown) && (corner == topCorner) )
crossobj.style.top = '-' + popupHeight + 'px';
else if ( ((direction == rightLeft)||(direction == leftRight)) && (corner == topCorner) )
crossobj.style.top = '1px';
else if (corner == bottomCorner)
crossobj.style.bottom = '2px';

if (only_once)
only_once_per_browser = only_once;

if (only_once_per_browser)
{
// verify the presence of a cookie
if (showOrNot())
showIt(direction);
}
else
setTimeout("showIt("+ direction + ")",1030);
}

TinyScript
03-13-2009, 04:36 AM
here's a working random script. Notice the double quotes ", not single quotes ' encapsulating the html output. perhaps this is your problem.

You might have to leave out the double quotes and use single quotes for images and styles


I put these images into this random script to show you.
this is from codelifter.com, my favorite resource site. His examples are concise and easy to follow.


<script language="JavaScript">
// ==============================================
// Copyright 2004 by CodeLifter.com
// Free for all; but please leave in this header.
// ==============================================

var Quotation=new Array() // do not change this!

// Set up the quotations to be shown, below.
// To add more quotations, continue with the
// pattern, adding to the array. Remember
// to increment the Quotation[x] index!

Quotation[0] = "Time is of the essence! Comb your hair.<img src='Image.jpg'>";
Quotation[1] = "Sanity is a golden apple with no shoelaces.<img src='Image.jpg'>";
Quotation[2] = "Repent! The end is coming, $9.95 at Amazon.<img src='Image.jpg'>";
Quotation[3] = "Honesty blurts where deception sneezes.<img src='Image.jpg'>";
Quotation[4] = "Pastry satisfies where art is unavailable.<img src='Image.jpg'>";
Quotation[5] = "Delete not, lest you, too, be deleted.<img src='Image.jpg'>";
Quotation[6] = "O! Youth! What a pain in the backside.<img src='Image.jpg'>";
Quotation[7] = "Wishes are like goldfish with propellors.<img src='Image.jpg'>";
Quotation[8] = "Love the river's \"beauty\", but live on a hill.<img src='Image.jpg'>";
Quotation[9] = "Invention is the mother of too many useless toys.<img src='Image.jpg'>";

// ======================================
// Do not change anything below this line
// ======================================
var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
function showQuotation(){document.write(Quotation[whichQuotation]);}
showQuotation();
</script>

sfraise
03-13-2009, 04:57 AM
Took the ' out of don't and changed to double quotes but still no luck.

Maybe someone with fresh eyes will find it and see something we're missing.

TinyScript
03-13-2009, 05:00 AM
Hey, I got your script to at least get some output



<script type="text/javascript" >


var array=new Array();
array[0]="<table border='0' width='100%'><tr><td valign='top'><img src='http://www.oohya.net/im.jpg' width='62' height='77'></td><td valign='top'>Hey we live close to each other.<br>U Single?<br><br><a href='http://www.oohya.org' target='_blank'>Reply</a></td></tr></table>";
array[1]="2";
array[2]="3";
var num=Math.random()*(array.length)


var html_code = array[num];
buildPopup_HtmlCode(200, 140, 'IM from: sweetlips69 F/23', html_code);





</script>


but unfortunately, i don't think html_code is being fed the right stuff. I tried to give it just simple text and it wouldn't come up. The array won't even show a value using an alert. I think you need to start over with what the last value in build popup is supposed to be. The hotlips stuff prints out, but I tried using single quotes and some text, but it didn't show up.

TinyScript
03-13-2009, 05:03 AM
BTW, I never saw the popup scroll up in the example you posted above. I'm using firefox

sfraise
03-13-2009, 05:04 AM
Are you using the scrolling_popup.js file along with it?
<LINK REL=StyleSheet HREF="scrolling_popup.css" TYPE="text/css">
<script type="text/javascript" language="javascript" src="scrolling_popup.js"></script>
<script type="text/javascript" >

sfraise
03-13-2009, 05:05 AM
I might have been working on the script at the time.
It should be working now, does for me anyway and I'm using firefox.
It's on a 30 second delay.

sfraise
03-13-2009, 05:15 AM
I added the code you just gave and see what you mean.
The pop up scrolls up and shows the title and stuff but where the html should be it says undefined.
It works fine though when I just use the one option without an array.

Ok, so then I changed the


buildPopup_HtmlCode(200, 140, 'IM from: sweetlips69 F/23', html_code);

To:


buildPopup_HtmlCode(200, 140, 'IM from: sweetlips69 F/23', array);


When it popped up it had both options in the one box.
Hey, it's closer anyway lol.
Basically this is telling me that we're not defining the var html_code correctly, but shouldn't that be what the var html_code = array[num] is doing?

Old Pedant
03-13-2009, 05:15 AM
TinyScript: This line is wrong:

var num=Math.random()*(array.length)

Need to use

var num=Math.floor(Math.random()*(array.length) );

If you are counting on the conversion of floating point to integer in
var html_code = array[num];
to do Math.floor(), it won't.

Try this code fragment:


<script>
var ar = new Array( "a","b","c","d" );
alert( ar[2] ); // shows "c"
alert( ar[1.75] ); // shows "undefined"
</script>


sfraise: See above for why you get "undefined". It's an array element reference error.

*******************

If anybody cares, the reason you get undefined for a result when you don't use an integer array element is simple: JavaScript arrays are *naturally* BOTH simple element-numbered arrays *AND* associative arrays.

That is, you *can* do things like:


var people = new Array( );
people["Ann"] = "Adams";
people["Bob"] = "Bones";
...
alert( people["Bob"] ); // and thus display "Bones"


What you might miss is that you can *ALSO* do this:


<script>
var ar = new Array( );
ar[1.25] = "a";
ar[3.5] = "b";
ar[1.75] = "c";
alert( ar[1.75] );
</script>

And so, when your code using ar[ someFloatingPointRandomNumber ] executes, it is looking for that exact floating point number as a *KEY VALUE* in an associative array.

Make sense?

TinyScript
03-13-2009, 05:23 AM
[QUOTE=Old Pedant;792724]TinyScript: This line is wrong:

var num=Math.random()*(array.length)

Need to use

var num=Math.floor(Math.random()*(array.length) );

If you are counting on the conversion of floating point to integer in
var html_code = array[num];
to do Math.floor(), it won't.

Thanks for the tip. I forget little stuff like that.
Could you also use parseInt(Math.random()*(array.length)) ?

TinyScript
03-13-2009, 05:32 AM
And so, when your code using ar[ someFloatingPointRandomNumber ] executes, it is looking for that exact floating point number as a *KEY VALUE* in an associative array.

Make sense?

YES YES YES!! thank you very much for that super explanation. You have just helped me to learn a very important lesson. I'm going to read about more about it later.

I had thought Math.random returned integers. I just looked it up and realized it returns a little less than 1. LOL you learn something new every day... I just wish it wasn't something so rudimentary. hehhehe

Old Pedant
03-13-2009, 06:06 AM
Could you also use parseInt(Math.random()*(array.length)) ?

Yes. parseInt of a floating point number does the same thing as Math.floor( ).

If you don't mind a further exposition, may I show you the flaw that your use of Math.round has, here:


var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
function showQuotation(){document.write(Quotation[whichQuotation]);}

Let's take his simple example of 3 quotations, so that the length of the array is 3.

So your code would do:

var which = Math.round( Math.random() * 2 );
Now, as you observed, Math.random() always returns a number >= 0 and < 1.
So if you multiply that by 2, you get a value that is EVENLY DISTRIBUTED in the range from 0 to JUST LESS THAN 2.

Now round that number.

So if the number is 0 to 0.499999999 it rounds to 0.
If the number is 0.5 to 1.499999999 it rounds to 1.
If the number is 1.5 to 1.999999999 it rounds to 2.

I think you can see that a result of 1 is thus TWICE AS LIKELY as a result of either 0 or 2.

And the same is true no matter what the array length: The first and last elements of the array are half as likely to be picked as any of the other elements.

I'll leave it to you to figure out why Math.floor( ) and parseInt( ) work correctly.

sfraise
03-13-2009, 06:47 AM
Old Pendant you rock!

Thanks to both of you guys for all of the help, working like a champ now.

sfraise
03-13-2009, 07:01 AM
So now let me ask this,
If I wanted to make variations of the 'IM from: sweetlips69 F/23' then I should be able to just make a 2nd similar array right below the current arrays correct?
In doing this, in the new array how do I set it up as to not conflict with the first one? I'm assuming it would be var array2 = new Array(); then add the [0],[1],[2] options and end it with the var num=Math.floor(Math.random()*(array.length) );.

But how do I define it to replace the 'IM from: sweetlips69 F/23' in the buildPopup_HtmlCode(200, 140, 'IM from: sweetlips69 F/23', html_code);?

Could I do something like something = array2[num] then just replace 'IM from: sweetlips69 F/23' with 'something'?

Old Pedant
03-13-2009, 07:44 AM
Why not just replace it with otherArray[otherNum]???



var otherArray = new Array(
"bananabreath X/77",
"cucumbercrash A/09",
"duckduckgoose G/33" );
var otherNum = Math.floor( Math.random() * otherArray.length );
...
buildPopup_HtmlCode(200, 140, 'IM from: " + otherArray[otherNum], html_code);
...

sfraise
03-13-2009, 08:27 AM
Nope, won't come up as is at all, it will come up if I put an ' after 'IM from: " + otherArray[otherNum] but it just shows the string as the title.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum