Solved

Javascript - Popup Once

Posted on 2010-11-09
6
646 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

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

Accepted Solution

by:
hosneylk earned 500 total points
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now