Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Javascript - Popup Once

Posted on 2010-11-09
6
Medium Priority
?
657 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This video teaches viewers about errors in exception handling.
Six Sigma Control Plans
Suggested Courses

648 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