Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra #lightbox elements when reloading page with ajax #517

Closed
jpardi opened this issue Apr 12, 2016 · 5 comments
Closed

Extra #lightbox elements when reloading page with ajax #517

jpardi opened this issue Apr 12, 2016 · 5 comments

Comments

@jpardi
Copy link

jpardi commented Apr 12, 2016

In my site, my images are loaded via ajax. Two things I have found:

  1. Once the page loads, I must call lightbox.init() for the plugin to do its magic. Is calling init() correct in this use case?
  2. When repetitively refreshing the images, lightbox creates a #lightbox element upon each ajax call. This causes a problem in that an empty lightbox appears in the page after the second ajax call. To fix this, I modified the build() function as below, which seems to fix the problem.
var lbox = $('#lightboxOverlay');
if (lbox[0] == undefined) {
    $('<div id="lightboxOverlay" class="lightboxOverlay"></div><div id="lightbox"  ....... </div>').appendTo($('body'));
}

Does this make sense? If so, can this fix be incorporated into the master code?

@NaiDev
Copy link

NaiDev commented Jun 10, 2016

Hi, how do I implement that piece of code into my js file?

@timkingman
Copy link

I hit a similar issue trying to upgrade from 2.51 to 2.8.2.
I still had the lightbox script included in the head, which meant the init method was running before I even had any page content at all. The docs tell you to add the lightbox script at the end of body, which fixes this issue. The click handlers are attached to body, so you don't need to run init() again after loading ajax content.

My patch recommendation would be to defer init() to run after the dom loads, which used to happen in a $(function(){ } ) block at the bottom of 2.51, but runs on script load in 2.8.2.

@leiz-me
Copy link

leiz-me commented Apr 26, 2017

Same issue. Using the trick that @jpardi provided to make it work.

@lokesh
Copy link
Owner

lokesh commented Nov 26, 2017

Chiming in on this older issue.

If anyone else is seeing this issue they can call lightbox.enable() instead of lightbox.init(). The enable method attaches the click events to the newly added images but does not construct any new DOM elements to append to the page.

In the next release I'll be adding a code check to prevent the duplication of the DOM elements. Multiple init() calls will not cause any side effects.

#615

@lokesh
Copy link
Owner

lokesh commented Nov 26, 2017

@lokesh lokesh closed this as completed Nov 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants