0

I have a javascript which opens up a window just before a surfer enters my site which has 24 hours cookie meaning it will popup again after the surfer visits my site again after 24 hours. Right now one can only close the div if the red "x" button is pressed. Also when I run it I can only add images or iframes but I'd like o add some text like "welcome to my site".

Here is the code

var floatingAd={
    run: function (e, t) {
        if (e === 2 || e === 1 && (this.getCookie("floatingAd") === null || this.getCookie("floatingAd") === "")) {
            document.write('<div id="floatingAdFixed" style="bottom: 0px; background: #fff; font-family: Arial, Helvetica, sans-serif; left: 0; padding: 0px 0; position: fixed; font-size: 16px; width: 100%; z-index: 99999; float: left; vertical-align: middle; margin: 0px 0 0; opacity: 0.88; font-weight: bold;">');
            document.write('<div style="text-align: right; width: 710px; margin: 340px auto;"><img onclick="floatingAd.close(' + e + ');" style="position: absolute; margin-top: -11px; margin-left: -23px; cursor: pointer;" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAACXBIWXMAAABIAAAASABGyWs+AAAACXZwQWcAAAAYAAAAGAB4TKWmAAAABmJLR0QA/wD/AP+gvaeTAAAF4klEQVRIx7VVbUxTVxg+5972ln5CK/KNAhMFhQ0pBbqg0fFHnPthZGKyxMSYzE1xJo4ax4xz0/lDlhjRqPHHSNQsMTNmP9wi8QOcRgEBFUFEGLRIQT6kpdBC23vO2Xvu1DgVsz9r+vbe3p7zPs/7vM97itD//MKvP2gvL5870tFxEDHGTMnJP+SdOTPwrgR3d+xImujq2stzxa9YUblo9+7RWRc/3LrVXF9cfHXg1Ck6WFPDrixb1tS6cWPCrMm3b0+4VlzcNHbtGh2prWXX16y52nPkiPmtFTzYtMk85nReTF+71m7QapEAMYMxajt5st2Smroq9/TpwVc3tm7ZkuDt7r60dO/eLF1cHEKiiHwuF3p4+PCthJUrP0mvqPC8BGgpK4ueGBo6997q1R8ZBAEJksREvR6LBgOaBqnunTjRDCClANKvJN+8eZ6nt/d8bmWlTRcdjRiCtyBgpFIhX38/66yurou2WsuWHDgwJvINpTpdxfwVKzbp/X6MKUUC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QzK4ja+ReTWwT4YIjY0hjVotyrKcxnMrAHqt9vjw48d1gtWqSMEBQALM7ckWLUJ9bW0tMqWf2uvr3TxClJbyZygjAw5fqqyH5MpeMS8PDXd31+k0muP/avIfOTlm8PrFxJSUD+WmJjgYMZYKC9GAy/XA6fOVfN7Z6X7VccczMxMXmEyXkubNywrevs1PPCwWFKBBl+sWCYfXfNzW5nnDpr9mZ5t1KtX55PT0lXBMIrfT2QzJ137R1eV+2xAdW7iQg/yWmJpq5ZYZ6Omp84dCpes7OjyzDtpncXHp7xsMB/lANXq9P44T8iQATvJB0yegHwSADWCCSLAu//eLVqmSC6OivoVE+P7UVOUvT592v3OSCywWy2Q4LMJBgJSA5AFI7gdHzXDLwhqYa6SHwdMDiAQgInyHUUQmtZo0eTzjr+b7G6/KDfXpZXvRAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDEwLTAyLTEwVDEyOjQyOjM1LTA2OjAw75HxegAAACV0RVh0ZGF0ZTptb2RpZnkAMjAwOS0wNi0yNFQxOTo0MzozMi0wNTowMBoOvs4AAAAASUVORK5CYII="></div>');
            document.write('<div id="floatingAd" style="text-align: center; width: 710px; margin: 300px auto; ">');
            document.write("</div>");
            document.write("</div>");
            document.getElementById("floatingAd").innerHTML = t
        }
    },
    close: function (e) {
        if (e === 1) {
            this.setCookie("floatingAd", 1, 1)
        }
        document.getElementById("floatingAdFixed").style.display = "none"
    },
    setCookie: function (t, n, r) {
        var i = new Date;
        i.setDate(i.getDate() + r);
        var s = escape(n) + (r == null ? "" : "; expires=" + i.toUTCString());
        document.cookie = t + "=" + s + ";path=/"
    },
    getCookie: function (e) {
        var t = document.cookie;
        var n = t.indexOf(" " + e + "=");
        if (n == -1) {
            n = t.indexOf(e + "=")
        }
        if (n == -1) {
            t = null
        } else {
            n = t.indexOf("=", n) + 1;
            var r = t.indexOf(";", n);
            if (r == -1) {
                r = t.length
            }
            t = unescape(t.substring(n, r))
        }
        return t
    }
}
/**
 * Run the script
 * 1 = 24 hours cookie, 2 = refresh
 */

floatingAd.run(1, '');

Two questions:

  1. How do I make it to close if you click anywhere on the screen?
  2. Hwo do I make it to show text in the floatingAd.run(1, '');?
Ayman Safadi
  • 11,502
  • 1
  • 27
  • 41
  • If my answer addressed your question, don't forget to choose it as the correct one (bug checkmark under the voting buttons)... well, only unless this is not the correct answer or someone provides a better one, of course! – benomatis Mar 29 '14 at 12:59

1 Answers1

0

For 1 check out How do I detect a click outside an element?

EDIT: If you would like to have the window closed if "you click anywhere on the screen", just use your object in the following way:

$('html').click(function() {
    // You may skip the following if statement, it may work without it
    if (document.getElementById("floatingAdFixed").style.display != "none")
        // The following line is what you'll definitely need
        floatingAd.close(1);
});

For 2, just add it as another variable in run like this

var floatingAd={
    run: function (e, t, text) {
    // ...
    document.write('<div id="textContainer">' + text + '</div>');
    // ...
}

Then use it like this:

floatingAd.run(1, '', 'Writing some text');

DEMO HERE

Community
  • 1
  • 1
benomatis
  • 5,536
  • 7
  • 36
  • 59
  • Hi Webeno! For no 2 it worked perfectly. For no 1 I read the link but my coding knowledge is close to 0 so I can't understand how to do it... Can you please kindly place the code here so I can paste it? Thanks! – user3475615 Mar 29 '14 at 16:52
  • do you want the window to close if you click anywhere at all or only outside the window that popped up? – benomatis Mar 29 '14 at 18:03
  • I edited my answer for 1, hope it will work for you! – benomatis Mar 29 '14 at 20:54