Making sure that CSS is implemented prior to executing JavaScript while utilizing jQuery.load

I have been developing a website where the content section is loaded dynamically when users click on links in the navigation bar. These links are connected to

jQuery.load(<some HTML page>)
.

One particular page that I'm having trouble with loads into this content area as a photo gallery (let's call it photos.html). Users can scroll through the images, and more photos are loaded dynamically. Here's how it functions:

<div id="scrolling_window">
    <div id="container">
        <ul id="list">
            <!-- Content loaded dynamically. -->
        </ul>

        <div id="loader">
            Page Loaded.
        </div>
    </div>
</div>

The scrolling_window has a fixed height set in css, while the container expands dynamically to fit all the images.

In my javascript code, there is a function that determines when the bottom of the scrolling window is nearing the end of the container. When this happens, more images are loaded. However, when accessing the gallery page directly by entering the URL manually, everything works fine. But when loading it within the existing page using jQuery.load, it doesn't function correctly.

I discovered that the issue lies in the sizing calculations in the javascript being based on CSS-defined container sizes. Some experimentation led me to these findings:

  • Manually opening photos.html in a browser only works if the CSS file is loaded before the javascript file.
  • When loading the content via jQuery.load, it functions properly if the scrolling_window is styled like
    <div id="scrolling_window" style="height:150px">
  • Loading the CSS file on the main page index.html also solves the issue when viewing the content dynamically.

Essentially, it appears that the CSS file must be loaded prior to executing the javascript...

Therefore, my question is... Is there a way to ensure that when loading an HTML page into an existing <div> element, the referenced CSS on the loaded page is loaded before the javascript runs?

Answer №1

In my opinion, the best approach would be to initially hide the div by setting its display property to none. Once the CSS and JavaScript have been loaded, you can then make it visible and activate it.

By hiding the div with display: none, it remains loaded behind the scenes. After the CSS loads, it receives its necessary styling while remaining invisible. Only after all the required JavaScript files have finished loading, it will become fully functional and responsive thanks to your JavaScript function.

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

Customize Individual Rows in a React DataGrid

I am exploring the world of Material UI React data grid for the first time, and I am looking to add a unique background color to only the initial three rows. Each row should have a different color scheme that remains static, without any interactions trigge ...

Repeated item replacing my model using ngOptions

I am struggling with an issue related to a select element in my HTML code. The select element has certain data attributes and ng-model bindings that should allow me to update the category when an item is selected. However, whenever I try to console log the ...

Show a particular attribute from an array

My goal is to create a function that displays only minivans from an array of cars when a button is pressed. However, I'm having trouble getting anything to display when the button is clicked, even though there are no errors in the program. Any advice ...

Is it possible to insert a variable into a span or v-tooltip tag?

I am currently using vue 2 along with vuetify 2, and I seem to be encountering an issue when attempting to display data from a table. Whenever I insert curly braces between the v-tooltip tags, it results in a blank page. Below is an example of the code: &l ...

Develop an Innovative Data-Driven Application using AJAX Technology

After inputting an artist's name and clicking on the button, I expect to receive a visually appealing list of their songs. I am encountering the following issue: Whenever I hit the button, no results are being returned, and I am unsure of the reaso ...

Enhance PHP Array for optimal performance with AJAX

I am facing a challenge with my PHP file that populates a page using AJAX. Despite my efforts, I have not been able to find a suitable solution. Below is the PHP code and its corresponding outputs: $result = mysql_query("SELECT id, product, picture FROM ...

The beauty of Aurelia's scoped CSS

Embarking on my Aurelia journey, I find myself navigating through the vast world of web development with tools like nodejs, gulp, and more. The Aurelia CLI has made it convenient for me to set up a visually appealing Aurelia project in Visual Studio Code ...

What is the method to set precise values on a range slider?

Currently, I am developing a PHP website that requires a slider for displaying the years of publications. In essence, I need the slider to navigate through the different years when the publications were released. // Database connection and other PHP code ...

place a stationary object within a confined space

I have a specific element called #listMDMap that needs to be positioned within the row div. <div class="col-md-6"> <div class="row"> <div id="listMDMap" [style.position]= ...

Hovering over an element and eliminating any extra space

Hey there, I'm looking for some assistance with removing the whitespace on hover. Below is the code along with a screenshot for reference: div.show_sizes_onhover { background: rgba(255,255,255,.9); bottom: -1px; color: #a1a1a1; left: 0; padding: 5px ...

`The function `setTimeout` throws an error when passed as a string.`

Here are two pieces of code that may look identical, but one works properly: function hideElement(obj) {setTimeout(function() {obj.style.display = "none";}, 20);} On the other hand, the second one results in error: obj is not defined: function hideEleme ...

Adjusting the size of an image in HTML according to its dimensions

While working on creating an online shopping mall page, I encountered an issue. A majority of the images uploaded to my database have varying sizes, so I needed to resize them all. However, when I resized them and set their heights to be the same, some of ...

What are the steps to extract information from a website that is located remotely?

Imagine there is a URL out there, for example: www.website.com/data.jsp This link contains the following JSON data: {"successful":"true","rows":[{"zip":"65472","user_id":"10843","name":"Rufio"}]} I am struggling to extract this data at runtime using get ...

Using Jquery to generate key value pairs from a form that is submitted dynamically

I'm still learning the ropes of jquery and struggling with a specific issue. I'm looking for a way to dynamically set key:value pairs in the code below based on form inputs that have variable values. The code works when I manually add the key:va ...

The onLoad() method fails to retrieve the accurate Text field input when text has been inserted using JQuery

I am attempting to transfer a value that has been entered using JQuery into a text field to an ASP.NET backend handler which looks at the respective ASP.NET control's Text property. After some investigation, I found that the $("#inputid").val(myn ...

The jQuery form plugin in IE9 does not produce a JSON response

Utilizing for handling my form submissions has been a breeze. I recently set up a form to upload images via Ajax, and everything is working smoothly except when it comes to IE9 :). I am encountering difficulties in receiving the JSON response from the se ...

What could be the reason for the variable not being defined?

Initially, I have the following function: $.getJSON( 'getTerminalinsideCircle.json', { centerLatitude: adressMarker.getPosition().lat(), centerLongitude: ...

Learn how to perform a post request in node js without triggering a page redirect or refreshing the form, all without using the preventdefault method

Is it possible to prevent the page from refreshing after sending data through an HTML form hitting a specific endpoint? I'm unsure about the best solution as there is no prevent default in nodejs, and I don't want the same page to redirect or re ...

Struggling to Fetch Data from REST API with ng-repeat in Angular JS

After making a REST call from Angular JS and receiving a response from the API, I am looking to display messages on the front end using ng-repeat in my controller. $http({ url: "http://localhost:8080/services/AddConfig", method: "POS ...

Challenge with Pressing Follow/Unfollow Options

I'm facing an issue with my program that handles follow/unfollow functionality. Everything seems to work fine, but when I refresh the page only the first user in the row retains the correct button state. For example, if I follow user1, user2, and user ...