I'm not much of a jQuery coder (more of a PHP coder), except to call a few get values out of class and id and Ajax calls. However, I need to make a checkout system work and one thing I would like to do is to put a loading wait while I send a form through Ajax. When it's finished, it would redirect the user.
So on click:
- load the loading spinner
- fade the background out
- Ajax call
- when Ajax is done submitting a form which indirectly redirects.
Here is my code, following why I don't like it:
jQuery.fn.center = function () {
this.css("position","absolute");
this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
return this;
}
$( ".buttonFinish" ).on('click', function(event) {
$('body').fadeOut('slow');
$('#loader').center();
$('#loading').center();
$('#loader').show();
$('#loading').show();
//Use the above function as:
var request = $.ajax({
url:'{{ URL::route('saveCart') }}',
type:'POST'
});
request.done(function(msg) {
$('.order_number').val(msg);
$('#summaryForm').submit();
event.preventDefault();
});
return false;
});
Let's get a few questions out of the way: {{UR::route('')}}
is laravel syntax to call the routing name for URL. loader/loading are two div that I have for the spinner one is text the other is an animation through CSS. (I'd rather CSS it than to load a pix, but I am aware that loader.gif can be small)
My issue (and feeling) that this can be better. I am using CSS with display:none to hide my two div, and then I call fadeOut
, show, show, and center center function to center my loading Ajax in order to center things. Is there a compact way to achieve all the 4 steps needed in a clean way?
body
element, which hides everything. It doesn't matter that you call.show()
on something later: it'll remain hidden becausebody
is hidden. \$\endgroup\$body
element. Again, everything you see on the page is inside thebody
element, so if you hide the body, you hide everything. \$\endgroup\$