www.webdeveloper.com
Results 1 to 4 of 4

Thread: Need help with popup script to make it popup only once per browser session.

  1. #1
    Join Date
    Feb 2009
    Posts
    6

    Need help with popup script to make it popup only once per browser session.

    I want to configure the script below to popup only once per browser session. I know nothing about javascript, so I would appreciate any help anyone can give me. I have googled the hell out of tis and just can't find a clear answer. I appreciate it.



    Code:
    var tmr;
    var t;
    var obj;
    
    function sFa() {
    	obj = gObj();
    	sLft();
    	shw(true);
    	t = 0;
    	sTmr();
    }
    
    function hFa() {
    	t = -100;
    	sTmr();
    	return false;
    }
    
    function sTmr() {
    	tmr = setInterval("fd()",20);
    }
    
    function fd() {
    	var amt = Math.abs(t+=10);
    	if(amt == 0 || amt == 100) clearInterval(tmr);
    	amt = (amt == 100)?99.999:amt;
      	
    	obj.style.filter = "alpha(opacity:"+amt+")";
    	obj.style.KHTMLOpacity = amt/100;
    	obj.style.MozOpacity = amt/100;
    	obj.style.opacity = amt/100;
    	
    	if(amt == 0) shw(false);
    }
    
    function sLft() {
    	var w = 240;	// set this to 1/2 the width of the fa div defined in the style sheet 
    			// there's not a reliable way to retrieve an element's width via javascript!!
    					
    	var l = (document.body.innerWidth)? document.body.innerWidth / 2:document.body.offsetWidth / 2;
    
    	obj.style.left = (l - w)+"px";
    }
    
    function gObj() {
    	return document.getElementById("fa");	
    }
    
    function shw(b) {
    	(b)? obj.className = 'show':obj.className = '';	
    }

  2. #2
    Join Date
    Dec 2008
    Posts
    488
    Session variables are not visible to client side scripting, so you can do one of two things:

    1. Create a session variable on the server side that will only output the pop script once if the session variable isn't set (as in literally output the script to the browser).

    2. Create a cookie that expires at the end of the session.

    The advantage of using session variables server side is that it's faster and doesn't rely on user settings. If the user has cookies turned off, and your javascript simply checks to see if the cookie is there before creating the popup, it will still pop up every time.

    If you don't have access to session variables, or don't know where to start, my recommendation would be to attempt to create a cookie to a value like "false" or something. Then, check to see if the cookie exists. If it does, check to see if it's set to "false", and if so, set it to "true" and run your popup script. Then each time a page is loaded, it will check to see if the cookie exists and is set to true. If so, it will not execute the pop up script:

    Code:
    function popUp(url) {
        //...popup script here...
    }
    
    function testCookie() {
        if (document.cookie.indexOf("popupOnceValue=false") > -1) {
            document.cookie = "popupOnceValue=true;";
            return true;
        } else {
            return false;
        }
    }
    
    function popupOnce() {
        if (document.cookie.indexOf("popupOnceValue") > -1) {
            if (testCookie()) {
                popUp("http://www.domain.com/blah/");
            }
        } else {
            document.cookie = "popupOnceValue=false;";
            if (testCookie()) {
                popUp("http://www.domain.com/blah/");
            }
        }
    }
    
    function init() {
        popupOnce();
    }
    
    window.onload = init;
    Setting a cookie with no expiration date will cause it to expire when the browser is closed (unless the user has a session saver addon).

  3. #3
    Join Date
    Feb 2009
    Posts
    6
    could not get this to work. I put my entire code from first post in here
    function popUp(url) {
    //...popup script here...
    }
    And I put my base url for my site in the two spots here
    function popupOnce() {
    if (document.cookie.indexOf("popupOnceValue") > -1) {
    if (testCookie()) {
    popUp("http://www.domain.com/blah/");
    }
    } else {
    document.cookie = "popupOnceValue=false;";
    if (testCookie()) {
    popUp("http://www.domain.com/blah/");
    }
    }
    }


    But nothing pops up now.

  4. #4
    Join Date
    Dec 2008
    Posts
    488
    Can you post the new version of the code so I can see how exactly you combined it? All you needed to do was use sTmr() (or fd(), i'm not sure which just yet) in place of popUp().... But I think that kind of eliminates the purpose of having a script that pops up only once, considering it looks like this script was created to pop up on an interval.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles