Steps for dynamically loading the content of a Bootstrap 4 Modal

I am currently in the process of developing a website that will showcase a wide range of images. The design is set up as a landing page with all content contained within the main HTML page - there is only an index.html file for now.

This website will serve as the official online platform for a Theater, featuring images related to their various shows. To provide an estimate of the number of images:

  • There are about 40 shows, each with roughly 40 images per show (approximately 600kb per image).

That's nearly 1gb of pictures on a single page!

Each show is represented by an image, and clicking on the image opens a modal displaying additional related images in a grid format along with information about the show.

I am utilizing Bootstrap 4, jQuery, and CSS for this project.

The issue I am facing is that including all the modals with images directly on the HTML page causes the website to load slowly and not function properly.

I am exploring options to dynamically load images each time a user clicks on a show, rather than loading all pictures at once when the page initially loads.

While I have attempted jQuery injection, managing the opening and closing of modals has proved challenging, making it an inefficient coding method.

One solution I am considering involves manipulating images via a PHP server. PHP can process and optimize images, creating thumbnails to be displayed in the grid format so they do not load in full size until clicked (using lightbox.js). However, my knowledge of PHP is limited, and I have not found a simple tutorial for implementation.

I am open to any suggestions on how to approach this issue, especially those focused on SEO optimization and improving client-side loading speed!

<!-- MODAL EXAMPLE -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-full " data-dismiss="modal">
    <div class="modal-content mx-auto">
      <div class="modal-body">
        <div class="column mx-auto">
          <div class="sidebar-box">
            <h2 class="title">SHOW TITLE</h2>
            <h2 class="subtitle"> SHOW SUBTITLE</h2>
            <p class="middle">MIDDLE INFORMATION</p>
            <p class="description">SHOW DESCRIPTION</p>
          </div>

        </div>
        <div class="column mx-auto">
          <!-- THERE WILL BE ALL THE IMAGES IN A GRID-->
          <div class="row justify-content-center">
            <a href="stages/alesi/alesi1.JPG" data-title="Photography ©" data-lightbox="alesi">
              <img src="stages/alesi/alesi1.JPG" alt="" />
            </a>
            <a href="stages/alesi/alesi2.JPG" data-title="Photography ©" data-lightbox="alesi">
              <img src="stages/alesi/alesi2.JPG" alt="" />
            </a>
            <a href="stages/alesi/alesi3.JPG" data-title="Photography ©" data-lightbox="alesi">
              <img src="stages/alesi/alesi3.JPG" alt="" />
            </a>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

Thank you sincerely to the entire Stack Overflow community!

Answer №1

To implement this technique, you can utilize AJAX to load the content of each modal from a separate HTML or PHP file. Upon clicking on the modal, an AJAX request can be made to fetch and display the specific modal content within the page.

In essence, the entire <div class="modal-body"> section will reside in a distinct HTML file. When a modal is clicked, the following script can be executed:

$("the_modal").click(function(){
  $.ajax({url: "modal1.html", success: function(result){
    $("#myModal modal-content").append(result);
  }});
});

Additionally, you have the option to include data attributes on the modal and utilize them during the AJAX call to make it applicable for all modals across the website.

An additional improvement would involve optimizing images by reducing their size and quality.

Answer №2

Are you struggling with slow page loading times due to a large number of images? Consider implementing lazy-loading for your images to improve website performance. With lazy-loading, images will only load when they are visible in the viewport.

You can use a jQuery plugin called jQuery.Lazy() to easily implement lazy-loading on your website.

Answer №3

If you want to learn how to load dynamic data into a bootstrap modal, follow this link: https://getbootstrap.com/docs/4.3/components/modal/#varying-modal-content

When dealing with data stored in a database or JSON file, you can pass the clicked item's id using the data attribute as demonstrated in the example above.

By utilizing the show.bs.modal event, you can trigger an ajax call to populate the necessary data within the .modal-body. Upon closing the modal, you can then clear out the data.

Answer №4

It is not recommended to load 40 pictures of 600k by default as this may cause issues for mobile users. A better approach would be to utilize a gallery script such as Photo Swipe and implement a server-side script to retrieve a json file that will only create the gallery when requested. For more information on how to customize HTML slides in Photo Swipe, please visit:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for setting up a range slider for decimal numbers

I have implemented the following code for a range slider. It works perfectly fine for integer values like 1 and 100, but I am facing issues when trying to use decimal values. I attempted to substitute 1 with 0.1 but it did not yield the desired result. ...

What is the best way to adjust a fixed-width table to completely fill the width of a smartphone browser?

In an ancient web application, the design was primarily constructed using rigid tables: <div class="main"> <table width="520" border="0" cellspacing="0" cellpadding="0"> <tr> <td>...</td> </ ...

Convert the value to JSON format by utilizing the PHP GET method

After retrieving a value using the GET method in my PHP file, I am attempting to access it. Below is how my PHP file appears: <?php include 'Con.php'; header('content-Type: application/json'); $catid = $_GET["CatId"]; //array ...

I can't seem to figure out why I keep encountering a runtime error whenever I attempt to create a basic route in the latest version of nextjs, version 13.5

Encountering an error while attempting to create a basic route in app/dashboard/page.tsx. The error message suggests that the contents are not a valid react component, even though they conform to valid react component syntax. Unhandled Runtime Error Erro ...

Is there a way to trigger an ajax call specifically on the textarea that has been expanded through jQuery?

Whenever I expand a textarea from three textareas, all three trigger an ajax call. Is there a way to only call the ajax for the specific expanded textarea? I tried using $(this).closest('textarea').attr('id'), but it didn't work. A ...

Tips for importing a .geojson document in TypeScript using webpack?

I am trying to extract data from a .geojson file but faced some challenges while attempting two different methods: const geojson = require('../../assets/mygeojson.geojson'); The first method resulted in an error message stating: Module parse f ...

Steps for embedding the code into your website

I'm facing an issue with integrating a .jsx file into my website. I tried testing it on a single-page demo site, but nothing is showing up. Can someone guide me through the steps to successfully integrate it onto my site? I've also attached the . ...

What is the reason that custom styling for a single react component instance does not function properly?

In my current view, I have integrated a react component as shown below: <Jumbotron> Lots of vital information </Jumbotron> I am looking to give this particular instance a unique style, like using a different background image. However, addin ...

Activate the clicked tab in the Bootstrap navbar when selected

I have gone through similar posts, but I am unable to get mine to work. My goal is to activate the clicked tab. Here is what my bootstrap navbar looks like in HTML: <div class="navbar navbar-default" role="navigation"> <div class="container" ...

What is the best way to use selenium to extract all p-tags and their corresponding h2-tags?

I am looking to retrieve the title and content from an article: <h2><span>Title1</span></h2> <p>text I want</p> <p>text I want</p> <h2><span>Title2</span></h2> <p>text I want< ...

Sending images as a base64 string from a Titanium app to a Ruby on Rails web service

I am encountering an issue when trying to upload an image from an app that has been converted into a base64 string to a Ruby on Rails server. The app is developed using Titanium. However, after retrieving and decoding the image string back into an image, ...

Parameterized Azure Cosmos DB Stored Procedure

I am currently learning about Azure Cosmos Db, and I am in the process of developing a simple JavaScript stored procedure that will return a document if a specific Id is provided. However, when I run the stored procedure, I do not receive a "no docs foun ...

Unable to locate "Gruntfile.js" Node module for task execution

I am currently in the process of developing a module that enables node to execute Grunt tasks via the command line. This Node module is globally installed at : C:\Users\pcharpin\AppData\Roaming\npm\node_modules\task-app ...

Having trouble redirecting PHP errors to a separate log file instead of the Apache log file

Trying to configure a Ubuntu web server [LAMP] to write PHP errors into a php_error file instead of the Apache log, but encountering difficulties. Here's the troubleshooting steps taken: Edited the php.ini file: error_reporting = E_ALL | E_STRI ...

Is there a way to transform NextJS typescript files into an intermediate machine-readable format without having to build the entire project?

I need to deliver a Next.js project to my client, but I want to modify the TypeScript files so they are not easily readable by humans. The client will then build and deploy these files to their production environment. How can I achieve this? In summary, C ...

Is there a way to send arguments to a pre-existing Vue JS application?

A Vue application we've developed connects to a Web Service to retrieve data. However, the URL of the web service varies depending on the installation location of the app. Initially, we considered using .env files for configuration, but realized that ...

Utilizing the arr.push() method to replace an existing value within an array with a new object, rather than simply adding a new

Seeking help to dynamically render a list of components that should expand or shrink based on values being added or removed from an array of custom objects. However, facing an issue where pushing a value into the array only replaces the previous value inst ...

Learn how to fetch the attribute value using JQuery

In my current HTML, generated dynamically via JavaScript, I have the following code snippet: <div lang="en" id="s7_videoview_playPauseButton" class="s7playpausebutton" data-description="Scene7ComponentHolder" data-component="PlayPauseButton" state="up" ...

Steps for creating an HTML report using Intern JS

Our team relies on intern JS for automating functional tests, however we are facing difficulty in generating an html report. I attempted to use locvhtml as suggested by the Intern documentation (https://theintern.github.io/intern/#reporter-lcov), but unfo ...

Having issues with the POST method in node.js and express when connecting to a MySQL database

My GET method is functioning perfectly I have a database called stage4 and I am attempting to insert values into it from a frontend page The connection is established, I'm using Postman to test it first, but it keeps returning a "404 error" which is ...