473,385 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

rotating random popups

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
4 2336
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

52
by: Harlan Messinger | last post by:
Can you help me figure out what to do about popups? Sometimes we develop web applications where popups make very good sense for precisely the same reasons they make sense in traditional...
1
by: Sandy Bremmer | last post by:
I have seen many Javascripts that rotate images with each load or refresh of the page but so far all I've found require hard coding the image filename into the script. Does anyone know of a script...
31
by: Royal Denning | last post by:
I am designing a table with 2 columns and 20 rows. I want to insert small images (each with a link) and a text title in each cell of the table. On refresh of the page, I would like to have the...
10
by: Sehboo | last post by:
Hi, I am looking for code which can rotate pictures without refreshing the page. I want to read randomly selected 5 images from database and then keep on rotating them every 5 seconds. ...
2
by: sgMuser | last post by:
Hi, I am not a good developer of Javascript codes. Needs this help to make some modification to this famous free javascript from Anarchos. i am using this in one of my webpage. What it does is,...
3
by: duffint | last post by:
Hi there, I have this script that I need some direction in; it's mangled my head a bit. I want to be able to stick links around six random words; these links are then popup ads. I saw kind...
1
by: Sunshine192 | last post by:
Hi, I'm still kinda new to PHP so I could do with some advice. I'm using the code below to select and show a random image from a folder of images. rotate.php <?php // Make this the relative...
1
by: AR123 | last post by:
Hi I want to set up a rotating banner. Not sure how to incorporate my rotating banner code into the code below. I want the rotating banner to be the main feature image? This is set up in...
17
by: nspader | last post by:
Now that I have your attention ;) Here's what I would like to do. I would like to design Access to automatically generate a monthly schedule. Here's the catch, I have three shifts (8-4,10-6, and...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.