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

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

Your email address will not be published. Required fields are marked *