views:

162

answers:

2

I created a page that has a JQuery based dialog using the standard JQuery UI function. I do this with out of the box functionality of JQuery... nothing special at all. Here is my HTML for the dialog:

<div id = "myDialog">
    <!-- ... more html in here for the dialog -->
</div>

Then the JQuery called in javascript that transforms the <div> to a dialog:

    // pruned .js as an example of kicking up a JQuery dialog
    $('#myDialog').dialog({
        autoOpen: false,
        title: 'Title here',
        modal: true
        }
    });

Again, plain-vanilla JQuery. So you start this wizard by clicking on a link on the parent page, and it then spawns a JQuery dialog which has a significant chunk of HTML that includes images, etc.

As I continued developing this page, I started to notice that when I loaded the page in the browser that the <div> tags I was putting in that JQuery transforms into dialogs would very briefly be displayed. Then the page would act as expected. In other words, the dialog would not be hidden, it would be displayed briefly in-line in the page. Quite ugly and unprofessional looking! But after a split second, the page would render correctly and look just as I expected/wanted.

Over time, as the page size grew, the time the page would remain incorrectly rendered grew. My guess is that the rendering engine of the browser is rendering the page as it is loading, then at the end it is kicking off the JQuery that will transform the <div> into a dialog. This JQuery function will then transform the simple <div> to a JQuery dialog and hide it (since I have the autoOpen property set to false). Some browsers <cough>IE</cough> display it longer than others. My large-ish dialog now causes the page to incorrectly render for about 1 second... YUCK!

A: 

I came up with a resolution to this problem which works OK, but I'm wondering if someone knows of a better way.

The problem is that the browser renders the DOM as it loads it, then fires the JQuery .js at the end which hides it. So what I'm doing is turning the visibility off in a parent <div> tag so all the dialog content is hidden by default, then turning that parent <div> tag in .js.

<div id="bodyDiv" style="visibility:hidden"> 
    <div id = "myDialog">
        <!-- ... more html in here for the dialog -->
    </div>
</div>

then the JQuery .js:

<script type="text/javascript">

$(document).ready(function() {
    //turn the visibility on last thing
    $("#bodyDiv").attr("style", "visibility:visible");
});
</script>

as you can see, I just by default turn off the visibility of everything so the page renders w/o displaying the dialog contents, then I turn the visibility on again. This is not a perfect solution because it can still make the page act funny for a second but at least the dialog HTML isn't displayed in-line. The UX with this fix is acceptable, though not ideal.

Kevin Won
I did quite a bit of searching for a solution to this but found none, so I'm asking/answering this question myself so others don't have to feel the pain.
Kevin Won
+2  A: 

You can add some CSS so it's by default hidden, no onload code needed:

#myDialog { display: none; }

With this, no other code is necessary, when you open the dialog it'll reverse this style...so nothing additional to run on document.ready. Alternatively, if you have lots of dialogs, give it a class, like this:

<div id="myDialog" class="dialog"></div>

With this CSS:

.dialog { display: none; }

In almost all cases, jQuery uses the display style attribute to hide things, so using this to initially hide something will work with whatever you want to use the element for later, whether it's a dialog, a simple .fadeIn(), etc.

Nick Craver
works like a charm. thx.
Kevin Won