How to initialize Lightbox on page load?

delay for testing purpose is set to 3 sec, and cookie expire for 1 day (i know, i can delete temp files too during testings, but …)
in chrome/firefox you see “only” the overlay, because there is 3 long posts and the announcement is bellow them, so, you see a huge overlay and if you scroll down, will see the announcement too at the very bottom of the page.

in blogger we have 2 ways to add codes:

[list][]a designer, as you can see here: http://img.rating-widget.com/blog/10/blogger-layout-page.png, and you can add elements by adding a “html/javascript” widget, or
[
] you can insert your code, right inside into the template, anywhere you want:
http://rating-widget.com/blog/wp-content/uploads/2012/11/template-notes.png and http://rating-widget.com/blog/wp-content/uploads/2012/11/find-post-template-edited.png
[/list]
(this images are just examples, i found them easily so, i’m not made new ones)

you can find easily the popup code viewing my source code, because i didn’t changed any expression.

a possible and quick solution is probably to drop the centering function

      jQuery.fn.center = function () {
        var w = $(window);
        this.css("position","absolute");
        this.css("top", Math.max(0, ((w.height() - $(this).outerHeight()) / 2) 
          + w.scrollTop()) + "px");
        this.css("left", Math.max(0, ((w.width() - $(this).outerWidth()) / 2) 
          + w.scrollLeft()) + "px");
        return this;
      }

and set margins for the announcement div, but i would prefer your solution because it’s more elegant and offers the same feeling on every screen settings.

Ok, for the center() function to work, change the positioning from “absolute” to “fixed”:

jQuery.fn.center = function () {
    var w = $(window);
    this.css("position", "[COLOR="#FF0000"]fixed[/COLOR]");
    this.css("top", Math.max(0, ((w.height() - $(this).outerHeight()) / 2) + w.scrollTop()) + "px");
    this.css("left", Math.max(0, ((w.width() - $(this).outerWidth()) / 2) + w.scrollLeft()) + "px");
    return this;
}

var hasSeenTerms = $.cookie("terms");
if (!hasSeenTerms) {
    setTimeout(function () {
        $('<div>', {
            id: 'overlay'
        }).appendTo('body');
        $("#announcement").fadeIn('slow').center();
    }, 3000);
}

Does that solve the center bug?
If so we can look at the cookie/close functionality after that.

Demo

Yes, thank you, this solve the centering issue for firefox and chrome.
Can you take a look for the IE issues too, especially why IE8 “forget” about the pop-up?

So I finally got IE8 up and running and you’re right - it doesn’t work.
If you look at the JS console we see the error message:

HTML Parsing Error: Unable to modify the parent container element before the child element is closed (KB927917)
Line: 0
Character: 0
Code: 0
URI: http://gombakereso.blogspot.de/

This can be caused when JS tried to append something to a DOM element that has not finished loading.

In our code we have:

if (!hasSeenTerms) {
  setTimeout(function () {
    $('<div>', {
      id: 'overlay'
    }).[B]appendTo[/B]('body');
    $("#announcement").fadeIn('slow').center();
  }, 3000);
}

Try wrapping that in a document.ready callback:

$(document).ready(function(){
    jQuery.fn.center = function () {
        var w = $(window);
        this.css("position", "fixed");
        this.css("top", Math.max(0, ((w.height() - $(this).outerHeight()) / 2) + w.scrollTop()) + "px");
        this.css("left", Math.max(0, ((w.width() - $(this).outerWidth()) / 2) + w.scrollLeft()) + "px");
        return this;
    }

    var hasSeenTerms = $.cookie("terms");
    if (!hasSeenTerms) {
        setTimeout(function () {
            $('<div>', {
                id: 'overlay'
            }).appendTo('body');
            $("#announcement").fadeIn('slow').center();
        }, 3000);
    }

    $("#close").click(function (e) {
        localStorage.setItem("hasSeenTerms", "true");
        $.cookie("terms", true, {
            expires: 1,
            path: '/'
        });
        $("#announcement").remove();
        $("#overlay").remove();
        e.preventDefault();
    });
});

Does that help?

a dumb question meantime: in jqeury or JS <div> not need to be closed by a </div>? marked with red in the code … Its possible to be this the troublemaker, and the initial code to work even without this wrapping?

No, this is a standard way of creating elements in jQuery :slight_smile:
This will create:

<div id="overlay"></div>

my question wasn’t a random question.

as i mentioned we have 2 ways to insert codes into blogger template:

[list][] trough widgets
[
] trough template[/list]
the first way means, i can take the code as it is and insert it anywhere except the body,
as example: if i insert it into the right sidebar without appending it to an already existent code, this will cause a “line break like” blank space, as you can see it in the attached image.

BUT

if i try to avoid this, and go to the template code and try to insert this code into the <body> code anywhere here </body>, blogger don’t accept it, commenting that the <DIV> need to be closed.
the only “open” <div> is the marked one, i mentioned above.

Nonetheless, this is the standard way to create elements using jQuery :slight_smile:

If it makes you happy, you can also change it to this:

setTimeout(function () {
  $('<div id="overlay"></div>').appendTo('body');
  $("#announcement").fadeIn('slow').center();
}, 3000);

Did you try using $(document).ready() as I mentioned in post#24?

not yet, i waited for you reply regarding the <div> issue.
i can test it only later when i have access to IE8

<div id=“overlay”></div> seems to work too into the widget section,
it is saved into template too, but only works if is declared as widget (so, there is not matter if is as it was or with closing </div>).

regarding $(document).ready() i can confirm so far, that it seems to working on mozilla, chrome, IE9 and IE10
later i can test it also on IE8

let’s take it step-by-step:
pop-up seems to work correctly on IE8 too, i mean it pop-up, and can be closed by it’s button.

BUT

either on chrome, mozilla or IE, the cookie seems to be broken now. I mean it not store its value. I have this code:


    var hasSeenTerms = $.cookie("terms");
    if (!hasSeenTerms) {
        setTimeout(function () {
            $('<div id="overlay"></div>').appendTo('body');
            $("#announcement").fadeIn('slow').center();
        }, 3000);
    }

    $("#close").click(function (e) {
        localStorage.setItem("hasSeenTerms", "true");
        $.cookie("terms", true, {
            expires: 1,
            path: '/'
        });
        $("#announcement").remove();
        $("#overlay").remove();
        e.preventDefault();
    });

and no matter what browser i use, once i agree “my terms” and close the browser, with every new session i get the pop-up again. (on current session seems to work). So, the problem it’s possible to be that we declare var hasSeenTerms but as cookie we use just “terms” and should be the same?

Man, where did that call to localStorage come from?
I see that it was in the code I quoted in post#24 - I didn’t notice it. Sorry about that.

Anyway, as we said before, if you want to have the popup appear every 30 days or so, then you can set a cookie and have it expire after the desired period of time. You don’t need localStorage.

The reason you were seeing the popup again and again is that you set the expires attribute to 1.
You’ll need to set it to 30.

Also, I’m not sure if this might have been causing an issue, but I changed the cookie value from true to “true” (the first had no quotes).

And finally, the markup for your popup may be problematic.

You had:

<a id="close" style="text-decoration: none; color: #fff;">
  <div id='disclclose'>  
    <b>I Agree</b>   
  </div>
</a>

Although HTML5 allows <a> elements to contain block elements (like divs), running your page through the W3C validator produces 256 errors and 107 warnings, so I changed the markup slightly.

Here’s the revised code:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Disclaimer exampel</title>
    <style>

    /* disclaimer (pop-up window) */
    #disclhead {
      text-align: left;
      border: 1px solid #FF5500;
      border-radius:5px;
      -webkit-border-radius: 5px;
      -moz-border-radius: 5px;
      padding: 10px;
      background: #007800;
      background: -moz-linear-gradient(#00dd00, #007800);
      background: -webkit-linear-gradient(#00dd00, #007800);
      background: -o-linear-gradient(#00dd00, #007800);
      font-size: 20px;
      width: 378px;
      color: white;
    }

    #disclclose {
      text-align: center;
      border: 1px solid #007800;
      border-radius:5px;
      -webkit-border-radius: 5px;
      -moz-border-radius: 5px;
      padding: 3px;
      background: #FF8D54;
      background: -moz-linear-gradient(#FF8D54, #BC4001);
      background: -webkit-linear-gradient(#FF8D54, #BC4001);
      background: -o-linear-gradient(#FF8D54, #BC4001);
      font-size: 16px;
      color: #fff;
      display: block;
      width: 98%;
      cursor: pointer;
    }

    #disclclose:hover {
      background: #007800;
      background: -moz-linear-gradient(#00dd00, #007800);
      background: -webkit-linear-gradient(#00dd00, #007800);
      background: -o-linear-gradient(#00dd00, #007800);
    }

    #overlay{
      position: fixed;
      top: 0%;
      left: 0%;
      width: 100%;
      height: 100%;
      background-color: black;
      opacity:.80;
      -moz-opacity: 0.8;
      filter: alpha(opacity=80);
      z-index:1001;
    }

    #announcement{
      display: none;
      position: absolute;
      width: 400px;
      padding: 0 16px;
      border: 2px solid #007800;
      border-radius:5px;
      -webkit-border-radius: 5px;
      -moz-border-radius: 5px;
      background: #fff;
      padding: 10px;
      text-align: justify;
      font-size: 12px;
      z-index:1002;
    }

    #announcement p{
      margin: 10px 5px; 
      font-size: 16px;
    }
    </style>
  </head>

  <body>
    <div id="announcement">
      <div id='disclhead'>Terms of use</div>
      <p>
        Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. 
        Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel 
        eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan 
        et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi...
      </p>

      <a id="disclclose">I Agree</a>
    </div>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="http://ciupercomania.googlecode.com/svn/trunk/jquery.cookie.js"></script>
    <script type='text/javascript'>
      $(document).ready(function(){
        jQuery.fn.center = function () {
          var w = $(window);
          this.css("position", "fixed");
          this.css("top", Math.max(0, ((w.height() - $(this).outerHeight()) / 2) + w.scrollTop()) + "px");
          this.css("left", Math.max(0, ((w.width() - $(this).outerWidth()) / 2) + w.scrollLeft()) + "px");
          return this;
        }

        var hasSeenTerms = $.cookie("terms");
          if (!hasSeenTerms) {
          setTimeout(function () {
            $('<div id="overlay"></div>').appendTo('body');
            $("#announcement").fadeIn('slow').center();
          }, 500);
        }

        $("#disclclose").on("click", function(e) {
          $.cookie("terms", "true", { expires: 30, path: '/' });
          $("#announcement").remove();
          $("#overlay").remove();
          e.preventDefault();
        });
      });
    </script>
  </body>
</html>

And here’s a demo.

HTH

I tested it on IE 8 and it works fine :slight_smile:

apologize to continuously bother you, but this code seems to not work again.

setting expire to 1 means 1 day, it suppose to be long enough for testing purpose, but 30 days is good as well (this is not an issue)

blogger.com has a lot of conflicts if you check it with W3C validator, even when you pick the default blank template … so, our code should be fine as it is, but this is not a problem, i’ll adapt the inside text for your markup. (this is not an issue as well)

now let’s take a look again for your demo (my demo is the same). this works on current session but if you close and open the browser again, the pop-up raise once again. Test it with mozilla/chrome instead on IE (microsoft products are always too full of bugs) and you should have the same experience as me. (the cookie somehow is not remembered or recalled).

Unless your code is doing something unusual, https://developer.mozilla.org/en-US/docs/Web/API/document.cookie

;expires=date-in-GMTString-format If not specified it will expire at the end of session.

See [Date.toUTCString()](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/toUTCString) for help formatting this value.

1 is not a date in GMT string format.

We’re using a cookie plugin to simplify things (although it’s not strictly necessary, I grant you :))
The syntax is (where the value for “expires” is the number of days):

$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });

Really? It works fine for me.
Let’s get another opinion.
@Mittineague ; could you open this page using Firefox or Chrome.
You should see a “terms and conditions” popup when the page loads.
If you shut your browser, then reopen it and visit the page again, do you see the popup for a second time (as ciupercomania is experiencing)?

all examples i found, set this

expires: [COLOR="#FF0000"]a number;[/COLOR]

reading again what you show at https://developer.mozilla.org/en-US/docs/Web/API/document.cookie and try to set this in expires=date-in-GMTString-format, i found that i need to specify something like this one

expires=Fri, 09 May 2014 10:00:00 GMT;

right?
So, if i don’t want a specific date, but i want let’s say 30 days expire from when someone reach my page, and this pop-up renew itself by default after 30 days pass for another and another … cycles? how can i do this?

I think we posted at the same time :slight_smile:
Read post#35

yes :slight_smile:
unfortunately i testing it on 3 PC’s with different configurations, and i have the same experience, every time i close the browser, on reopening i get the pop-up again and again :frowning:

Are you testing locally?

locally, you mean i save it under a html file for testing? NO,
i testing on both links: your demo and my blogger test blog. (both on external server for me)