free hosting   image hosting   hosting reseller   online album   e-shop   famous people 
Free Website Templates
Free Installer

Random Popup Windows II




This is similar to Random Popup Windows I, except:
  • the popup generation is not really random, but a predetermined sequence
  • any number of popups can be used by simply adding to the URL arrays
  • A cookie is used to determine how many times the visitor has accessed the page:
    Previous Visits Cookie Value:
    On the first visit, popup1 appears, and popup3 appears on exit.

    On the next visit, popup2 appears, with popup4 on exit. And so on.

    When the final URL from the array has been accessed, the cycle repeats.


    Compatability

    All Javascript browsers which allow popups.

    Paste the following into the head section of your page:
    Now add this to your body tag:

    How It Works

    On load, the function doCookie() is called.

    This sets a cookie on the user's computer which tracks the number of visits to that page.

    With each visit, the cookie value is updated.

    The actual count value is retrieved by the function gettimes().

    This value is returned by the functioned, then assigned to the global variable num:
    count = document.cookie.substring(countbegin, countend);
    if (count > 0)
    {
    return (count);
    }
    return ("0");
    .
    .
    .
    num = gettimes(); 
    
    The popup sequencing is handled by 2 functions: enterpop() and exitpop().

    Both work in much the same way.


    All URLs for the popups are stored in an array:
    var newurl=new Array()
    newurl[0]="page1.html"
    newurl[1]="page2.html"
    newurl[2]="page3.html"
    newurl[3]="page4.html"
    newurl[4]="page5.html"
    
    
    This array may be expanded as required, simply by adding more elements.


    Next, the cookie value is interpreted:
    if (num == 0)
     {
     idx = 0
     }
    if (num > 0)
     {
     num2 = (num % newurl.length)
     }
    if (num2 == 0)
     {
     idx = newurl.length - 1
     }
    else
     {
     idx = num2 -1
     }
    
    where:
  • num is the cookie value
  • idx is the index of the URL array
  • num2 is the processed value of the cookie
  • newurl.length is the size of the URL array
  • The line num2 = (num % newurl.length) does most of the work.

    This gets the modulus, or remainder value, of the cookie value when divided by the array length.

    So if the cookie value is 21 and the array length is 5, the remainder of 21 divided by 5 is 1.

    This used to allow the popups to be sequenced in cyclic fashion, regardless of URL array size.


    This value, minus 1, is then assigned to the variable idx, since the array start at index zero.


    Finally, the popup is generated with the required parameters:
    var mylocation=newurl[idx]
    
    var winl = (screen.width-300)/2;
    var wint = (screen.height-500)/2;
    settings='height=500,width=300,top='+wint+',left='+winl+',
     scrollbars=0,toolbar=0,location=0,status=0,menubar=0,
     resizable=0,dependent=0,copyhistory=0'
    {
    MyPopUp = window.open(mylocation,"",settings)
    }
    }
    
    The function exitpop() works in a similar way.

    However, it skips a URL and loads the array element 2 steps down.

    This ensures that on the next visit, the previous visit's exit popup is not repeated.

    Thus, the sequence works like this (based on a total of 5 popups):

    Visit No.

    1
    2
    3
    4
    5
    6
    Entry Popup

    1
    2
    3
    4
    5
    1
    Exit Popup

    3
    4
    5
    1
    2
    3


    Try It Out

    Refresh this page a few times, and take note of:
  • the cookie counter value
  • the page numbers of the popups loaded on entry and exit




  • © 2002 CyberSchool
    Last Update: