3

When I use $(document).ready(function() {...}, the function is called after the DOM is ready, but (I think) before CSS styles have loaded.

I want to retrieve the width of an element, but when I try to retrieve it inside a ready() event, the value returned isn't matching the CSS value. However, when I use a timer to call this function 10 seconds later, it works as intended.

So, is there a way to listen to CSS rendering with jQuery?

Thank you

3
  • Can you provide a demo jsfiddle.net to show the problem? CSS should be loaded before DOM ready fires. Is there some animation occurring on the element, or those around it? Commented Oct 27, 2012 at 12:41
  • try $(window).load(function() { }). It fires after document ready. Commented Oct 27, 2012 at 12:42
  • Are you using external fonts? Commented Oct 27, 2012 at 12:44

2 Answers 2

11
$(window).bind("load", function() {
   // code here
});
Sign up to request clarification or add additional context in comments.

Comments

-1

To complete my first post, I use JSF framework (Mojarra) + PrimeFaces components. My all JS is this one :

$(document).ready(function() {      
    alert("parent : " + $("#j_idt14\\:panelGrid label").parent().css("width"));

    window.setInterval(function(){alert("parent : " + $("#j_idt14\\:panelGrid label").parent().css("width"));}, 5000);

});

and I get 416px followed 5s later by 522px.

I'm calling JS script via JSF like this :

<h:outputStylesheet name="styles/styles.css" />

And...that's all. I don't understand

1 Comment

Could the problem be the JSF lifecyle...?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.