Show picture during the process of loading an external webpage

Utilizing a script to load an external page (external meaning a page within my website) inside a div. Loading the page takes time, so I want to display an image until the page is fully loaded.

JAVASCRIPT


function HideLoader() {
    $('#loader').css('display', 'none');
}

function ShowLoader() {
    $('#loader').css('display', '');
}

$(document).ready(function() {
    $('.reveal').on('click', function(e) {
        e.preventDefault();
        ShowLoader();
        var link = $(this).attr('href');
        $('.page').load(link);
        $('#leftmenu').css('width', '70px');
        $("#leftmenu b,li ul").hide();
        $('li').unbind('click');
        $(this).show();
        HideLoader();
    });
});

CSS


#loader {
  display: unset;
  width: 100px;
  height: 100px;
  margin: 100px;
}

HTML

<div id="loader"> <img src="ajax-loader.gif" alt="loading" /> </div>

ISSUE: Currently displaying the image before clicking the link.

I would like the image to appear only when I click on the link and disappear once the page is fully loaded.

Answer №1

To clarify...

For CSS, simply set display:none:

#loader {
  display: none;
  width: 100px;
  height: 100px;
  margin: 100px;

}

Javascript Update the following:

function ShowLoader() {
    $('#loader').css('display', 'block');
}

and also this:

 $('.reveal').on('click', function(e) {
        var $that = $(this);

        e.preventDefault();
        ShowLoader();
        var link = $(this).attr('href');
        $('.page').load(link, function(){
             HideLoader(); // this places it in the load callback, so that these actions
             $that.show(); // will occur when the load is finished
        });
		$('#leftmenu').css('width', '70px');
		$("#leftmenu b,li ul").hide();
		$('li').unbind('click');
    });

Answer №2

Your div is continuously visible. To make it invisible by default, you should modify your css accordingly:

#loader {
  display: none;
  width: 100px;
  height: 100px;
  margin: 100px;

}

The display: unset property simply reverts back to the default value which is visible.

Additionally, update your showLoader function to the following:

function ShowLoader() {
    $('#loader').css('display', 'block');
}

Answer №3

Ensure to specify the display value as 'block' in your showLoader 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

Is there a way to arrange the outcomes in a single line?

I need help displaying my results in three rows side by side in React/JavaScript using flexbox. Since I only have one CardItem, I can't just copy and paste it three times as it would show the same result in each row. Is there a way to achieve this wit ...

Implementing jquery-confirm in CodeIgniter: A Step-by-Step Guide

I'm having some trouble with the jquery-confirm plugin from . Everything seems to be working fine, but when I try to delete an item, the jquery pops up an alert. However, when I click "okay", it doesn't actually delete the item. I can't figu ...

Merge HTML/CSS with JavaFX's FXML and customize the style using CSS

Quick Summary: I have a cool button on CodePen that rotates and grows when hovered over using HTML/CSS. I want to incorporate this button into my JavaFX GUI, built with SceneBuilder and FXML files. The GUI currently features buttons numbered 1-4, and I wo ...

Having a fixed footer in JQuery mobile does not adjust its position downwards as intended

I attempted to move the footer down using the provided code below, but it doesn't seem to be working. I even went ahead and removed all standard text from the CSS file to eliminate any potential issues. It should normally shift downward with <div ...

Guide on transporting PNG files over the boundary

I am working with two specific css commands. code h1 { background: #000000; border-radius: 6px; color: #fff; display: block; font: 40px/60px "Sans", Inconsolata, "Lucida Console", Terminal, "Courier New", Courier; padding: 40px 40px; text-align: center; ...

Using the jQuery library to make a request to the PasteBin API with

Hey there, I'm currently working on incorporating jQuery $.post with the PasteBin API to generate a PasteBin page and obtain the URL it supposedly returns. This is what my code looks like at the moment: $('#send_code').click(function(){ ...

unable to insert logo into HTML

I am customizing an HTML template, but the dimensions of the logo in the template are smaller than my logo. Despite adding my logo to the template, it is not showing up. @media (max-width: 673px) { #logo { margin-left: 20px; } } #header #logo ...

Encountering an issue upon pressing the 'calculate' button

Currently, I am delving into express node.js and attempting to execute a straightforward calculator code. However, whenever I click on the button, I do not receive any response, and there are no errors in my code either. I find myself a bit lost in figurin ...

Unable to display button image when using both id and style class in CSS

During my transition from Java 7 to Java 8, I've encountered a peculiar issue. Here's a brief explanation: In my FXML file, I have a button defined with style class button-red-big and id btnInput: <Button id="btnInput" fx:id="btnInput" align ...

Guide on sending a JavaScript function to a Pug file using Node.js and Express

In my recent project, I developed a node app using express along with a pug file. The express app is configured to listen on port 3000 and render the pug file. One of the key functionalities involves fetching information from an external API. My goal is to ...

req.next does not exist as a function [END]

I am currently working on developing a website and I have encountered an issue in the dashboard stage. The error message TypeError: req.next is not a function keeps appearing, particularly when trying to create a subpage for the dashboard. I am utilizing t ...

Adjusting the Size and Spacing of Vuetify's Buttons

Why are buttons extending beyond the card width? https://i.stack.imgur.com/wVRoQ.png I'm facing difficulties customizing the buttons within the cards. I want to eliminate all padding so that the black border perfectly encloses the icon without any un ...

Creating a loading animation that appears when the submit button is pressed in C#

I am working on a MVC website and I thought it would be cool to have a spinning GIF appear when the submit button is clicked until the next view loads. Here is my current code, but unfortunately it's not working and I can't figure out why. <p ...

What is the process for implementing the sticky table header jQuery plugin?

I am looking to implement a sticky header on all tables in my web application, which is built on PHP. As the amount of data continues to grow, search results are fetching more records that may not be visible. I am primarily a PHP programmer and do not have ...

Kartik's gridview in yii2 has a unique feature where the floating header in the thead and tbody are

I'm looking to create a table gridview with a floating header, where the tbody and thead are not the same. The image appears after refreshing the page, before the modal is refreshed. After refreshing the modal, this modal is inside pjax and it sets t ...

Looking for an instance of a node.js ftp server?

I'm facing a challenge in creating a node.js application that can establish a connection with an FTP server to download files from a specific directory: Despite attempting to follow the instructions provided in the documentation for the ftp npm packa ...

CSS - Update BackgroundColor Depending on Child Element Color

Is there an official CSS way to change the background color based on the child element in HEX? For example: render() { return ( ... <span> <h3 style={{ color: item.color }}>Item Color</h3> </span> ...

Angular's use of ES6 generator functions allows for easier management of

Recently, I have integrated generators into my Angular project. Here is how I have implemented it so far: function loadPosts(skip) { return $rootScope.spawn(function *() { try { let promise = yield User.findAll(); $time ...

Resize a group of images to match the parent's width and height dimensions

I am working with a div that contains variously-sized images and is nested inside a parent container. <div id="parentContainer"> <div id="boxToScale"> <img src="http://placehold.it/350x150" /> <img src="http://placehold.it/150 ...

Preventing Adjacent Text from Moving Due to a Floated i Element

I have a div with a bootstrap icon positioned at the top right corner. However, I want to center the text horizontally without shifting the position of the icon. Is there a way to achieve this? See my sample code here: https://jsfiddle.net/x1Lvyu6t/3/ ...