Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

How to show loading icon till website loads ?

How to show loading icon till website loads ?

2 Answers

I im not sure, i dont work with JS or so really but what i think you migh look for is make an if statement and somehow check if the page is loaded . So if you can put if the page is not loaded, show loading icon which will be the first to load, and after the whole page loads the code, hide or disply none the loading icon.

Typically, this means you're making an ajax call, and it's just a matter of hiding and showing the loading icon. Here is an an example, you have on an ajax form submit

$('#form').submit( function( event ) ) {
  $.ajax({ 
     url: //where is this ajax going,
     data: //attach your data,
     beforeSend: function() {
         $('#loader').show();
     },
     complete: function() {
         $('#loader').hide();
     },
     success: function(data) {
       $('#info').append(data);
       //or redirect
       // or send an email
      // or sign up
     // or anything you want to do with a form submit event
    },
    error: function() {
       alert('Something went wrong');
    }
   });
});

You can of course bind the ajax call to whatever action you want. This is of the jQuery syntax of an ajax call. Ajax has a bunch of these callbacks built in, that get called when certain things happen. That's usually where I like to control a loading icon.