Solved

Javascript - Popup Once

Posted on 2010-11-09
6
649 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This is about my first experience with programming Arduino.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

770 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