Ensure that jquery.load is executed before the HTML content is loaded

Recently, I encountered a situation where I had a bootstrap navbar stored in a separate file, specifically named navbar.html. I utilized Jquery.load() to load this navbar into my HTML page.

At the bottom of my HTML page, I included the following code:

<script  type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script> 
$(function(){
   $("#navbar-partial").load("navbar.html"); 
});
</script>

The HTML page also contains

<div id="navbar-partial"></div>
.

Following the navbar-partial, there is a main-content div with the remaining content on the page. Unfortunately, the main-content loads before the navbar, resulting in a visually unappealing appearance. Although inserting the navbar code directly into each file would resolve this issue by allowing it to load first and quickly, I prefer to centralize the code in one file to avoid repetitive editing.

Do you have any suggestions on how to ensure that the navbar-partial loads faster or first? I attempted moving the script tags to the head section but to no avail.

Thank you for any help or advice!

Answer №1

The $(function(){ function waits for the DOM to be fully loaded before running.

To improve loading speed, make sure your code is placed below the HTML element it references like this:

<div id="navbar-partial></div>
<script> 
    $("#navbar-partial").load("navbar.html"); 
</script> 

Although there may still be a delay, this method can help speed up the process a bit :)

For a more efficient solution, consider handling this issue on the server-side. However, given your current setup with plain HTML files, this workaround will suffice for the time being.

Answer №2

Is this really the most efficient way to create a partial menu?

To speed up loading time, consider removing the 'dom ready' section.

<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>$("#navbar-partial").load("partial-navbar.html");</script>

Place this code snippet within the #navbar-partial element in your HTML file for it to function correctly.

Alternatively, you may want to explore implementing a server-side include feature, which is widely supported by most web servers. Simply use the following syntax:

<!--#include virtual="partial-navbar.html" -->

For more information on server-side includes, 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

What are the differences between performing a search in MongoDB (database component) and Node.js (server-side)?

1) My input consists of a string with approximately 30 comma-separated elements, such as str1, str2, str3, etc. This is represented by "INPUT_STRING". 2) Within my MongoDB collection, I have a set of "allowed_strings" which includes entries like str1, str ...

What is the best way to activate a click event when I set a radio button to checked?

I am facing an issue with an uninitialized property in my app.component.ts: color!:string; I am trying to automatically initialize the color property when a radio button is selected: <div> <input type="radio" name="colors" ( ...

AngularJS - Controller not recognizing state name parameter

I'm a newcomer to angular and struggling to understand why this specific issue is occurring. Interestingly, when I use {{$state.current.name}} in my view, everything works as expected. However, the problem arises when I attempt to pass it to my contr ...

Change the marquee scrolling back to its original starting point

I am trying to implement a continuous image scroll (marquee) in HTML. My goal is to have the marquee stop scrolling when the mouse hovers over it, with the images returning to their initial position. Once the mouse moves away, I want the marquee to resume ...

Creating a mouse hover effect for irregular shapes on an image map

Is it possible to implement a mouse hover effect on irregular shapes within an image map? These irregular shapes are not basic polygons like rectangles, triangles, or circles, but rather complex designs such as lanterns or animals. Can this be achieved? ...

Having issues with setting up nodejs on kali linux

Whenever I try to execute the configure script ./configure for nodejs installation, it fails to run successfully. Traceback (most recent call last): File "./configure", line 19, in <module> from distutils.spawn import find_executable ModuleN ...

When the text is in motion, the ::before element will stay static

I'm working on creating a unique custom SVG underline for a header design. My goal is to achieve something similar to this: https://i.sstatic.net/EI1aT.png Here's what my code currently looks like: HTML: <div class="container"> ...

Challenges arise when employing angular scope functions in the context of the DOM (html)

UPDATE: Encountered an issue caused by attempting to iterate over a function's return value using ng-repeat \ ng-options, rather than converting the data into a regular object through a promise. Here is the code snippet: $scope.layout.getParti ...

Having issues with templateURL not working in AngularJS routeProvider

I've been experimenting with ngRoute and I'm facing an issue with templateURL not working as expected. The functionality works perfectly except for when I attempt to use templateURL. Here are the 4 files involved: test.html <p>{{test}}&l ...

Stop users from submitting likes or dislikes more than once

JavaScript Logic $('button.like').click(function(){ $.get($(this).parent().attr('href'),function(data){ $(this).html(''+data); }); return false; }); View Function def like(request): if request.is_aj ...

Guide on aligning a child div directly beneath its parent without specifying the parent's height

I want to position a div beneath its parent in a way that mimics a menu opening below it. Currently, I attempted using right and top, but the child div ends up overlapping the parent. If the parent had a defined height (rather than top: 0 as shown below), ...

Highlight react-bootstrap NavItems with a underline on scroll in React

I am currently working on a website where I have implemented a react-bootstrap navbar with several Nav items. My goal is to enable smooth scrolling through the page, where each section corresponds to an underlined NavItem in the navbar or when clicked, aut ...

There seems to be an issue in Angular as it is unable to retrieve /

I'm encountering an issue with my simple application where I am receiving the error message "Cannot GET /." Also, in the console, I see this error: TypeError: Cannot read property 'checked' of null at Object.SL_BBL_locer. I'm unsure ab ...

"Implementing a form submission feature in React.js that dynamically applies a class to the result element

I recently developed a basic BMI calculator using React.js. I am now attempting to implement a feature where if the calculated BMI result falls outside the range of a healthy BMI, the result text will be displayed in red color (I am utilizing styled-compon ...

How to Toggle a RequiredFieldValidator on/off with a Checkbox in ASP.NET using C#.NET and jQuery/JavaScript

I am facing an issue with enabling or disabling a required field validator based on the checkbox status. The goal is to have the validator enabled when the checkbox is checked and disabled when it is unchecked. Additionally, when the checkbox is checked, a ...

Replacing jQuery.ajax from a different directory/domain - problem with using relative URLs

We are incorporating scripts from various sources and domains, including one that contains the definition for jQuery.ajax function. Calls to jQuery.ajax are being made from different places and domains using relative URLs, such as jQuery.ajax("my/relativ ...

Is it normal for the protractor cucumber tests to pass without observing any browser interactions taking place?

Having recently started using protractor cucumber, I have created the following feature. Upon launching protractor protractor.conf.js, the browser opens and immediately closes, displaying that my tests have passed. Is this the expected testing behavior? Sh ...

Utilizing the Jquery click function to assign an element as a variable

I'm currently working on a script that aims to extract the inner text of any clicked item with the class "customclass". Keep in mind that this specifically targets anchor elements. However, I've encountered an issue where the individual element ...

Transform the initial HTML table row into a header row for every table by utilizing XSLT

My XML file contains HTML content that I need to manipulate. Previously, I used <xsl:copy-of select="customFields/customField[@name='mainContent']/html"/> to bring the content to the correct location. Now, I have a new requirement to conver ...

The removeAttribute function has the ability to remove the "disabled" attribute, but it does not have the capability to remove

When it comes to my JavaScript code, I have encountered an issue with two specific lines: document.getElementsByName('group')[0].removeAttribute('disabled'); document.getElementsByName('group')[0].removeAttribute('checke ...