Skip to content

Nasri Messarra

Associate Professor, Senior Expert in Digital Communication

Menu
  • Home
  • Social Media
  • Office and Web tools
  • Multimedia
  • USJ
  • About Me
Menu

Simple way to auto-open and auto-close lightbox using Javascript

Posted on November 28, 2013May 6, 2023 by N.A.M.

The idea is to use lightbox to display a pop-up image or text after a page has loaded.

The first step is to reference the link for the lightbox popup. This can be done by adding an “id” to the hyperlink tag (in bold and red):

<a href=”Images/nye.jpg” data-lightbox=”NYE” title=”New Year’s Eve at the HangOut!” id=”NYE”>

The second step is to add the necessary Javascript code just before the closing HTML tag:

<script type=”text/javascript”>
$(document).ready(function () {
document.getElementById(“NYE”).click(); //simulates a link click
setTimeout(‘document.getElementsByClassName(“lb-close”)[0].click();’, 10000); //waits 10 seconds and then hides closes the lightbox window
});
</script>

This script was tested successfully on Firefox and Google Chrome for Android.

Leave a Reply Cancel reply

You must be logged in to post a comment.

Nasri Messarra
View this post on Instagram

A post shared by Nasri Messarra | Photography (@nasri.messarra)

© 2025 Nasri Messarra | Powered by Superbs Personal Blog theme