By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,481 Members | 1,213 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,481 IT Pros & Developers. It's quick & easy.

rotating random popups

P: n/a
A project I am working on requires 5 random popup windows that rotate. A
cookie is set each time one of the popups occurs with a 5 day expire period.
The visitor isn't supposed to see the same popup within that 5 day period,
so it needs to rotate to another popup. I have changed it around several
times (setCookie calls randomWin and vice versa) but now have brain burnout.
I'm probably complicating it - a bad habit I have.

My randoms work great, the cookies have landed, but I just can't seem to get
my brain around how to do the rotation. I'm thinking that if the cookie is
found, the array needs to be modified to no longer contain that page, but I
have tried various things incuding splice(), slice(), etc. with no luck.
Guidance? NOTE: the second popup is a popunder.

<script language="JavaScript">

var winArr = new
Array('72hr.htm',630,215,'72hr2.htm',400,150,'72hr 3.htm',630,205,'72hr4.htm',570,420,'72hr5.htm',295 ,260);
var r = randomNumber(winArr.length / 3);
var winUnder = winArr[3];
var winSrc = winArr[r * 3];
var winW = winArr[(r * 3)+1];
var winH = winArr[(r * 3)+2];

function randomNumber(limit){
return Math.floor(Math.random()*limit);
}

function setCookie()
{

var expDate = new Date();
expDate.setDate(expDate.getDate() + 5);
var index = document.cookie.indexOf(winSrc);

if(index == -1)
{
//create cookie
document.cookie=winSrc + "=" + winSrc + ";Expires=" + expDate.toString() +
";";
//display window
randomWin();
}
else
{
//find other window
}

}

function randomWin()
{

if (winSrc != winUnder)
{
window.open(winSrc, "aWindow", "width="+winW+", height="+winH).focus();
}
else
{
window.open(winSrc, "aWindow", "width="+winW+", height="+winH).blur();
}

}
</script>

Thanks,
Christina
Nov 26 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In comp.lang.javascript message
<ts******************************@centurytel.net >, Sun, 26 Nov 2006
14:39:31, Christina <de******@centurytel.netwrote:
>A project I am working on requires 5 random popup windows that rotate. A
cookie is set each time one of the popups occurs with a 5 day expire period.
The visitor isn't supposed to see the same popup within that 5 day period,
so it needs to rotate to another popup. I have changed it around several
times (setCookie calls randomWin and vice versa) but now have brain burnout.
I'm probably complicating it - a bad habit I have.

My randoms work great, the cookies have landed, but I just can't seem to get
my brain around how to do the rotation. I'm thinking that if the cookie is
found, the array needs to be modified to no longer contain that page, but I
have tried various things incuding splice(), slice(), etc. with no luck.
Guidance? NOTE: the second popup is a popunder.
As long as you are sure that at least one page will be allowed, for
choosing from such a small number it will be acceptable to repeatedly
choose at random until you find an acceptable choice.
Alternatively, scan the list of pages and put references to the good
ones in a new list, then choose at random from that[*].
><script language="JavaScript">
Deprecated
>var winArr = new
Array('72hr.htm',630,215,'72hr2.htm',400,150,'72h r3.htm',630,205,'72hr4
.htm',570,420,'72hr5.htm',295,260);
var winArr = [
['72hr.htm',630,215],
['72hr2.htm',400,150],
['72hr3.htm',630,205],
['72hr4.htm',570,420],
['72hr5.htm',295,260]
]

can lead to clearer code. NEVER use a naming format with unnecessary
exceptions; change 72hr.htm to 72hr1.htm. Unless the pages are numbered
1 to 5 elsewhere, 0 to 4 may well be better.
[*] Pseudo-JS :
A = []
for (J in winArr) if (winArr] is OK) A.push(winArr[J])
PopUp = A[randomNumber(A.length)]
>function randomWin()
{

if (winSrc != winUnder)
{
window.open(winSrc, "aWindow", "width="+winW+", height="+winH).focus();
}
else
{
window.open(winSrc, "aWindow", "width="+winW+", height="+winH).blur();
}

}
Avoid repeating necessarily-matching code. If that works, this should
work :-

function randomWin() {
with (window.open(winSrc, "aWindow", "width="+winW+", height="+winH))
winSrc == winUnder ? blur() : focus()
}
// or
function randomWin() { var W
W = window.open(winSrc, "aWindow", "width="+winW+", height="+winH)
winSrc == winUnder ? W.blur() : W.focus()
}
It's a good idea to read the newsgroup and its old FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/ Old RC FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Nov 27 '06 #2

P: n/a
Thank you for your response. Just FYI - I'm a student in a JS course and my
script is based on what has been provided and requested by the instructor.
Much of what I have is actually gleaned from research online, in books, etc.
that was not provided by the instructor. I think most of my problem is the
old chicken vs. egg theory - which came first - The array? The cookie? The
window?

"Dr J R Stockton" <jr*@merlyn.demon.co.ukwrote in message
news:jt**************@invalid.uk.co.demon.merlyn.i nvalid...
As long as you are sure that at least one page will be allowed, for
choosing from such a small number it will be acceptable to repeatedly
choose at random until you find an acceptable choice.
Unfortunately, that's not the case, since the instructions requested only 5
pages, but 7 days for the cookie to expire, there are 2 days where nothing
would pop-up if the person were to visit the site every day for a week. That
was going to be my last worry - I just didn't want the same page to pop up
within the first 5.
>
Alternatively, scan the list of pages and put references to the good
ones in a new list, then choose at random from that[*].
Can you clarify what you mean by 'put references to the good ones in the new
list'? Can't this be accomplished by using splice(start,end)? From
testing I've done it modifies the array by eliminating the current random
winSrc (and associated values): I tested it with a document.write:

var winArr = new
Array('pop0.htm',630,215,'pop1.htm',400,150,'pop2. htm',630,205,'pop3.htm',570,420,'pop4.htm',295,260 );
var r = Math.floor(Math.random()*(winArr.length/3));
var winUnder = winArr[3];
var winSrc = winArr[r * 3];
var winW = winArr[(r * 3)+1];
var winH = winArr[(r * 3)+2];

document.write(winArr +"<br />");
document.write(r +"<br />");
start=parseInt(r);
winArr.splice(start,3);
document.write(winArr +"<br />");
>><script language="JavaScript">
Deprecated
What should I use?
var winArr = [
['72hr.htm',630,215],
['72hr2.htm',400,150],
['72hr3.htm',630,205],
['72hr4.htm',570,420],
['72hr5.htm',295,260]
]
I've never seen this format for assigning array values. I'm not exactly
clear on how the values are pulled. Can you clarify?
[*] Pseudo-JS :
A = []
for (J in winArr) if (winArr] is OK) A.push(winArr[J])
PopUp = A[randomNumber(A.length)]
I don't understand what J represents in this. According to w3schools (and
other sites), push() adds to the array and I don't want to add to it. Can
you clarify this?
>
Avoid repeating necessarily-matching code. If that works, this should
work :-

function randomWin() {
with (window.open(winSrc, "aWindow", "width="+winW+", height="+winH))
winSrc == winUnder ? blur() : focus()
}
// or
function randomWin() { var W
W = window.open(winSrc, "aWindow", "width="+winW+", height="+winH)
winSrc == winUnder ? W.blur() : W.focus()
}
You're absolutely right...and normally I would have condensed it once I had
the rest of the code working. Haven't ever used '?', but did see a short
reference to it just this morning. I will research it further.

Thanks,
Christina
Nov 27 '06 #3

P: n/a
I've reworked the code and have it working so that the same page doesn't
pop-up twice Unfortunately this also means that no page pops-up if the
random number matches an array element that has already popped up, since it
has a cookie set. So how do I go about getting a new random number until it
finds a page that no cookie exists for? Obviously this would need to stop
once all of the pages have cookies set. I've tried a few looping concepts,
but so far none have worked. Here is what I have now:
var winArr = new
Array('pop0.htm',630,215,'pop1.htm',400,150,'pop2. htm',630,205,'pop3.htm',570,420,'pop4.htm',295,260 );
var lenArr = winArr.length/3;
var r = Math.floor(Math.random()*lenArr);
var winUnder = winArr[3];
var winSrc = winArr[r * 3];
var winW = winArr[(r * 3)+1];
var winH = winArr[(r * 3)+2];
var start=parseInt(r);
var expDate = new Date();
expDate.setDate(expDate.getDate() + 1);

var c_index = document.cookie.indexOf(winSrc);

if (c_index == -1)
{
setCookie()
showPop()
}
function setCookie()
{
//create cookie
document.cookie=winSrc + "=" + "pop"+ r + ";Expires=" + expDate.toString()
+ ";";

}

function showPop()
{
//display window
var W
W = window.open(winSrc, "aWindow", "width="+winW+", height="+winH);
winSrc == winUnder ? W.blur() : W.focus();
}
"Christina" <de******@centurytel.netwrote in message
news:ts******************************@centurytel.n et...
>A project I am working on requires 5 random popup windows that rotate. A
cookie is set each time one of the popups occurs with a 5 day expire
period. The visitor isn't supposed to see the same popup within that 5 day
period, so it needs to rotate to another popup. I have changed it around
several times (setCookie calls randomWin and vice versa) but now have brain
burnout. I'm probably complicating it - a bad habit I have.

Nov 28 '06 #4

P: n/a
In comp.lang.javascript message
<DJ******************************@centurytel.net >, Mon, 27 Nov 2006
12:46:31, Christina <de******@centurytel.netwrote:
>"Dr J R Stockton" <jr*@merlyn.demon.co.ukwrote in message
news:jt**************@invalid.uk.co.demon.merlyn. invalid...
>Alternatively, scan the list of pages and put references to the good
ones in a new list, then choose at random from that[*].

Can you clarify what you mean by 'put references to the good ones in the new
list'?
The star in brackets indicates what you should there refer to.
<FAQENTRY>
>>><script language="JavaScript">
Deprecated

What should I use?
<script type="text/javascript">
</FAQENTRY>

>var winArr = [
['72hr.htm',630,215],
['72hr2.htm',400,150],
['72hr3.htm',630,205],
['72hr4.htm',570,420],
['72hr5.htm',295,260]
]

I've never seen this format for assigning array values. I'm not exactly
clear on how the values are pulled. Can you clarify?
It is an array literal of 5 elements, each of which is an array literal
of 3 elements. In this case, the 5 elements are all similar, but the
grammar does not require that. Then winArr[3][1] is 570.
>[*] Pseudo-JS :
A = []
for (J in winArr) if (winArr] is OK) A.push(winArr[J])
PopUp = A[randomNumber(A.length)]

I don't understand what J represents in this. According to w3schools (and
other sites), push() adds to the array and I don't want to add to it. Can
you clarify this?
J is a counter, referring in turn to every element of winArr (above,
winArr] should be winarr[J] ). And A.push(X) pushes its argument X onto
A, by reference.

It's a good idea to read the newsgroup and its old FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/ Old RC FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Nov 28 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.