Link to home
Start Free TrialLog in
Avatar of patrickpst
patrickpst

asked on

Javascript - Popup Once

Hi, I have a javascript to open popup when the user clicks somewhere on the page, however, I need help...

I need a script open only once or after 5 days...

My script is attached... Where in my code I have to change?

Thanks
addEventHandler = function (element, eventName, eventHandler) {
    if (element.attachEvent) {
        element.attachEvent("on" + eventName, eventHandler)
    } else if (element.addEventListener) {
        element.addEventListener(eventName, eventHandler, false)
    } else {
        var oldEvent = element["on" + eventName];
        element["on" + eventName] = function () {
            if (oldEvent) {
                oldEvent()
            }
            eventHandler();
            return true
        }
    }
};
getName = function (arr) {
    var ret = '';
    //for (var i = 0; i < arr.length; i++) {
    //    ret += String.fromCharCode(arr[i])
    //}
    return arr
};
Cookie = function (cookiename) {
    var name = cookiename;
    this.create = function (value, domainName, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 100));
            var expires = "; expires=" + date.toGMTString();
            var domain = "; domain=" + domainName
        } else {
            var expires = ""
        }
        document.cookie = name + "=" + value + expires + domain + "; path=/"
    };
    this.read = function () {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') {
                c = c.substring(1, c.length)
            }
            if (c.indexOf(nameEQ) == 0) {
                return c.substring(nameEQ.length, c.length)
            }
        }
        return null
    };
    this.erase = function () {
        this.create(name, "", -1)
    }
};
var arrU = 'mysite';
var arrJ = 'mysite';
var windowName = 'propaganda';
var urlPublicidade = "(?:" + getName(arrU) + "|" + getName(arrJ) + "" + getName(arrU) + ")";
var urlDC = "http://www." + getName(arrU) + ".com/page.html";
var urlDU = urlDC;
var jaClicou = false;
var wProps;
var wOpen;
var excludes = [];
var u = "n.u.l.l";
var cookieName = "dcPopunder";
var cookie;
var vlrCookie;
var cookieDuration = 24 / 24;

function initPopunder(codContrato, urlExc, modo) {
    cookie = new Cookie(cookieName);
    vlrCookie = cookie.read();
    if (vlrCookie == null) {
        if (codContrato) {
            if (urlDC.toLowerCase().substr(urlDC.length - 12) == "codcontrato=") urlDC += codContrato;
            if (urlDU.toLowerCase().substr(urlDU.length - 12) == "codcontrato=") urlDU += codContrato
        }
        var w = screen.availWidth;
        var h = screen.availHeight;
        wProps = "width=" + w + ",height=" + h + ",toolbar=yes,location=yes,directories=0,status=yes,menubar=yes,scrollbars=yes,resizable=yes,top=0,left=0";
        if (modo == 'dc' || modo == 'du') {
            if (modo != 'du') {
                openPopunder()
            }
            if (!wOpen) {
                addEventHandler(document, "click", openPopunder);
                if (modo == 'du') {
                    addEventHandler(window, "load", brs);
                    addEventHandler(window, "beforeunload", ext)
                }
            }
        } else {
            if (urlExc instanceof Array) {
                excludes = urlExc
            }
            parseLinks();
            addEventHandler(window, "load", parseLinks)
        }
    }
};

function parseLinks() {
    var verifUrl = (excludes.length > 0) ? true : false;
    for (i = 0; i < document.links.length; i++) {
        var lnk = document.links[i];
        var bool = lnk.onclick ? true : false;
        if (verifUrl) {
            for (j = 0; j < excludes.length; j++) {
                bool = bool || (lnk.href == excludes[j])
            }
        }
        if (!bool && !lnk.href.match(urlPublicidade)) {
            addEventHandler(lnk, "click", openPopunder)
        }
    }
};

function openPopunder(urlDest) {
    if (!jaClicou) {
        if (typeof urlDest != 'string') urlDest = urlDC;
        if (urlDest != '') {
            wOpen = window.open(urlDest, windowName, wProps);
            if (wOpen) {
                wOpen.blur();
                cookie.create("1", document.domain, cookieDuration);
                jaClicou = true
            }
        } else {
            jaClicou = true
        }
    }
    return true
};

function ext() {
    if (!jaClicou) {
        var iie = document.getElementById("iie");
        try {
            iie.launchURL(urlDU);
            cookie.create("1", document.domain, cookieDuration)
        } catch (e) {
            openPopunder(urlDU)
        }
        jaClicou = true
    }
};

function brs() {
    document.body.innerHTML += "<object id='iie' width='0' height='0' classid='CLSID:" + u + "' type='application/x-mplayer2'></object>"
};
cookieName = 'pcmega';
initPopunder(124, [], 'du');

Open in new window

Avatar of hosneylk
hosneylk
Flag of Singapore image

the opening only once part is working I assume?

To make it work every 5 days you have to change the following:
in line 69
var cookieDuration = 24 / 24;

should be

var cookieDuration = 5;

and..

in line 29 (cookie create function):
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 100));

should be

date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));

(100 changed to 1000)

Open in new window

Avatar of patrickpst
patrickpst

ASKER

Hi hosneylk, I already tried to do that, when the browser closes, the script expires, instead to keep alive for 5, 24 or more days... I have tested it on firefox and I.E, both I get the same trouble. Test it by yourself to you see.

Thanks
IE doesn't seem to be creating a cookie for security reasons when you specify the domain property in your code.

http://blogs.msdn.com/b/ieinternals/archive/2009/08/20/wininet-ie-cookie-internals-faq.aspx

try creating the cookie this way..
document.cookie = name + "=" + value + expires;

Open in new window

I tried to do this, but problem on internet explorer still persists. Firefox looks ok.
ASKER CERTIFIED SOLUTION
Avatar of hosneylk
hosneylk
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi hosneylk, thank you for help me... This script is working fine, my i.e was with problem... I fixed it, now is ok. ^^