开发者

JQuery dialog momentairly displayed on page load

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 tim开发者_开发技巧e, 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!


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.


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.


I use CSS3 selectors with a bit of naming & tag convention. All my dialogs are <div> elements, and the id always ends with "dialog". I then use this CSS:

div[id$=dialog] { display: none; }

A sample dialog:

<div id="my-sample-dialog" title="Sample Dialog">
      <span>I'm invisible!</span>
</div>

In case CSS3 is not an option, you can use CSS2 to achieve the same result, but you have to be more careful not to use the dialog moniker in any <div> id not intended to be hidden:

div[id~=dialog] { display: none; }

and set your dialog id to something like "my sample dialog"


If you're using a "popup" rather than a "dialog" I had to do the following:

HTML

<div data-role="popup" class="popup">
  <!-- CONTENT -->
</div>

CSS

.popup { display:none; }
.ui-popup-container .popup { display:block; }

I initially hide the popup using display:none and after jQueryUI wraps the popup in a container, the other style takes precedence.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜