Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does browser rendering and JavaScript execution happen simultaneously?

For example, if I have this:

$('#button').click(function() {
    $.get('/question', function(data) {
        $('#question').html(data);
        $('#question').dialog( ... );
    });
    return false;
});

Will the user see the question content for a brief moment before the dialog is shown?

Note: Normally I'd just hide the #question manually, but there's actually a step in between html() and dialog() with another jQuery plugin where the content must not be 'hidden'.

like image 410
Joe Avatar asked Nov 14 '22 13:11

Joe


1 Answers

Short Answer

Yes, it's possible that the user will see the question content for a brief moment before the dialog is shown.

The Fix

To guarantee you won't momentarily see the contents of #question before displaying the dialog, absolutely position #question offscreen before displaying it. After that, call the jQuery plugin that requires #question to be displayed. Finally, hide #question and restore its position.

CSS

#question
{
    display: none;
}

JavaScript

$('#button').click(function() {
  $.get('/question', function(data) {
    var question = $('#question');
    question.html(data);
    var position = question.css('position');
    var top = question.css('top');
    var left = question.css('left');
    question.css({ position: 'absolute', top: -1000, left: -1000 }).show();
    //whatever you need to do with #question while it's not hidden
    question.hide().css({ position: position, top: top, left: left });
    question.dialog( ... );
  });
  return false;
});
like image 95
Chris Shouts Avatar answered Nov 16 '22 03:11

Chris Shouts