?
Solved

Javascript - Popup Once

Posted on 2010-11-09
6
Medium Priority
?
658 Views
Last Modified: 2013-11-23
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

0
Comment
Question by:patrickpst
  • 3
  • 3
6 Comments
 
LVL 10

Expert Comment

by:hosneylk
ID: 34099569
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

0
 

Author Comment

by:patrickpst
ID: 34099589
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
0
 
LVL 10

Expert Comment

by:hosneylk
ID: 34099858
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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:patrickpst
ID: 34101905
I tried to do this, but problem on internet explorer still persists. Firefox looks ok.
0
 
LVL 10

Accepted Solution

by:
hosneylk earned 2000 total points
ID: 34108142
it works for me.. check if the cookie is ever created in IE. You could use http://www.nirsoft.net/utils/iecookies.html. If not try creating the cookie with just name and value first..
0
 

Author Closing Comment

by:patrickpst
ID: 34109300
Hi hosneylk, thank you for help me... This script is working fine, my i.e was with problem... I fixed it, now is ok. ^^
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Progress
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question