What could be causing my jQuery script to not load properly on the first attempt?

I have a jQuery script set up on this particular webpage just before the ending body tag. Here is the script:

 <script>
   $(document).ready(function(){
     var demomain = $(".demo-bg section");
     var demomainW = demomain.height()+ 150;
     $('head').append('<style>.demo-footer{top:'+ demomainW +'px !important;}</style>');
     console.log("HEIGHT --> "+demomain.height());
   });
 </script>
</body>

The purpose of this script is to calculate the height of a specific section above the footer so that the footer can adjust its position using CSS accordingly. I attempted achieving this without jQuery, solely with plain CSS, but couldn't get it to work. Is there an alternative method using just CSS?

Despite my efforts, the script does not always load correctly initially (tested on various devices). I made sure to initiate the function within the document ready section, and yet, I'm unsure what might be causing this issue...

I would greatly appreciate any help or suggestions. Thank you.

Answer №1

If the link you shared is indeed the one causing the issue, simply include the following script tag beneath your jQuery script reference.

<!--jQuery-->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script>
       $(document).ready(function(){
         var mainSection = $(".demo-bg section");
         var mainSectionHeight = mainSection.height() + 150;
         $('head').append('<style>.demo-footer{top:'+ mainSectionHeight +'px 
      !important;}</style>');
         console.log("HEIGHT --> "+mainSection.height());
       });
    </script>

Answer №2

The script is loading properly, but the images and other content are not fully loaded which prevents the script from accurately determining the final size.

Furthermore, it is advisable to avoid using position fixed or absolute in favor of classic block positioning for the footer. Let the browser handle the positioning automatically for a more seamless layout.

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

How can we combine refs in React to work together?

Let's consider this scenario: I need a user to pass a ref to a component, but I also have to access that same ref internally. import React, { useRef, forwardRef } from 'react'; import useId from 'hooks/useId'; interface Props ext ...

What is causing the button on the update div to not respond when clicked after receiving a Jquery ajax response?

As a newcomer to Jquery, I am facing a challenge where I want the user to be able to select an item and have a table displayed with editable information. I expect this table to automatically update once the user clicks on the "modifier" button. The tables ...

use python selenium to retrieve text enclosed by two span elements

<li class="page-item active"> <span class="page-link"> "12" <span class="hjhs">(current)</span> </span> </li> After reviewing the code above, my objective is to extr ...

Utilizing JavaScript Callbacks in HTML Image Tags

Currently I am working on a small application and looking to include a YouTube section. I have come across a method for obtaining the user's YouTube icon: This is included in the head section of my code: <script type="text/javascript" src="http:/ ...

Popper.js failing to initialize during page load

After attempting to initialize Popper.js as shown below, I am encountering an issue where nothing is happening. $().ready(function(){ $('[data-toggle="tooltip"]').tooltip(); }); Interestingly, when I apply the same code to a button, it w ...

Learn how to utilize AngularJS Material to toggle the enable/disable functionality of a checkbox

Can someone assist me in enabling and disabling the checkbox as shown in the attachment image https://i.stack.imgur.com/l6g1C.jpg View the PLNKR angular.module('BlankApp', ['ngMaterial']) .config(['$mdThemingProvider', fun ...

Updating the favicon by replacing the image URL

I'm looking to customize the favicon on resource HTML pages. Specifically, when visiting the image URL, I want my own icon to display in the title bar instead of the server's icon (in this case XAMPP). ...

Learn how to dynamically import external modules or plugins using the import() functionality of TypeScript 2.4 within the production script generated by Angular CLI

Utilizing the typescript 2.4 [import()][1] feature has proven to be effective for dynamically loading modules. Testing this functionality has shown positive results, especially when importing modules and components located within the same directory. Howev ...

Transforming the MUI CircularProgress into a half circle shape

After utilizing CirculaProgress, I was able to achieve the following: https://i.sstatic.net/Y0Seo.png Is there a simple method to transform it into a semicircle like shown here? https://i.sstatic.net/D8bKu.png ...

How can we translate this php json_encode function into Node.js?

Seeking the equivalent Node.js code for this PHP Script: $SMA_APICall = "https://www.alphavantage.co/query?function=SMA&symbol=".$symbolValue."&interval=15min&time_period=10&series_type=close&apikey=R3MGTYHWHQ2LXMRS"; $SMAres ...

Adjusting the Scaling Value to Match the Browser's Scaling Value

I'm struggling with a problem in HTML where the initial-scale function is not working as expected. When I zoom in on certain pages, it saves the zoom level. However, when I navigate to another page and then return to the original one, the zoom level r ...

Is there a way to retrieve data from both JSON and a file simultaneously?

I'm trying to use JavaScript fetch API to upload a photo message with text to Discord webhook. How can I upload both my JSON data and file? var discordWebHookBody = new FormData() discordWebHookBody.append("map", map) discordWebHookBody.appe ...

Is Chrome Facing an Autofill Glitch?

Can someone explain this strange behavior? When using Chrome, clicking on an input field triggers autocomplete for "country." However, if you change "country" to "city," the autocomplete shifts to that field instead. This issue seems to be isolated to thi ...

Contrasting createMuiTheme and getMuiTheme

When would you choose to use one over the other? What are the key differences in how they operate? ...

Tips on converting HTML code into a data image URI

My question may seem unconventional, but here is what I am trying to accomplish. I want to create a design similar to the one found at the following link: I would like to embed text with an image and retrieve the data image URL using Ajax. I know how to g ...

Evaluate the advancement of a test using a promise notification for $httpBackend

I am currently utilizing a file upload feature from https://github.com/danialfarid/angular-file-upload in my project. This library includes a progress method that is triggered when the xhr request receives the progress event. Here is an excerpt from the so ...

Troubleshooting Bootstrap 4 problem with varying sizes of div content

I have encountered a problem with the script below, where the content in a div is of variable size and does not always occupy the entire horizontal space allotted by the col-6 class from one row to the next: <div class="card-body " > <div c ...

Convert h264 video to GIF using Node.js

Currently, I'm utilizing the "pi-camera" library to successfully record video in a raw h264 format on my Raspberry Pi. However, I am encountering an issue with the node.js library "gifify" which keeps throwing the error "RangeError: Maximum call stack ...

What is the best way to delete a particular tag using jQuery?

Illustration: htmlString = '<font><a>Test Message</a></font>'; updatedHtmlString = htmlString.find('font').remove(); Desired Result: <a>Test Message</a> This code snippet is not yielding the expe ...

The submitHandler constantly refreshes the webpage

Struggling to send a form to a PHP file upon submission and retrieving the value without having to reload the page. It was working smoothly for some time but now it just won't cooperate. I have multiple pages with similar functionality, where 2 work p ...