Bug Tracker

Ticket #1319 (closed bug: fixed)

Opened 7 years ago

Last modified 3 years ago

$().ready() sometimes fires too soon on Safari/Win

Reported by: dmethvin Owned by:
Priority: major Milestone: 1.4
Component: event Version: 1.3.2
Keywords: Cc:
Blocking: Blocked by:

Description

(It may also fire too soon on Safari/Mac but I can't test that.)

Go to this page in Safari/Win:

 http://methvin.com/jquery/splitter/vsplitter.html

Occasionally, it will not initialize properly--the right pane is too short by the width of a non-existent scroll bar. You may need to reload it repeatedly to see the problem.

I think this is due to one or more document properties being invalid at that point. If I remove the special-case Safari handling in jQuery's ready() method and let it drop into the onload case instead, the problem goes away.

According to this page, Safari fires onload *very* early, before the page has displayed, so that code is most likely overkill anyway.

 http://www.howtocreate.co.uk/safaribenchmarks.html

Attachments

jquery_test.html Download (3.5 KB) - added by davidserduke 7 years ago.
test case (slightly modified so can download)
safariready_test.html Download (951 bytes) - added by davidserduke 7 years ago.
simplified test case 2
slowcss.php Download (152 bytes) - added by davidserduke 7 years ago.
simplified test case 2 data
1319.diff Download (1.5 KB) - added by davidserduke 7 years ago.
patch
1319plus.diff Download (2.3 KB) - added by davidserduke 7 years ago.
patch plus opera change
jquery-example.htm Download (831 bytes) - added by darkangel 5 years ago.

Change History

comment:1 Changed 7 years ago by john

  • Version changed from 1.1.2 to 1.1.3
  • Component changed from ajax to event
  • Milestone changed from 1.1.3 to 1.1.4

I have not been able to duplicate this, even with your test page. Are you still able to duplicate it?

comment:2 Changed 7 years ago by akuzemchak

I've noticed the same thing happening on my site:

 http://thoughts.kuzemchak.net/

The archives area (under the main nav) should hide by default using $(function() {...}), but in Safari 3 it will still show unless the page is refreshed.

comment:3 Changed 7 years ago by shameister

Happened at  http://methvin.com/jquery/splitter/vsplitter.html in only one of nearly 50 calls, but really appears.

comment:4 Changed 7 years ago by john

  • Status changed from new to closed
  • Version changed from 1.1.3 to 1.2
  • Resolution set to worksforme
  • Milestone changed from 1.1.4 to 1.2.1

Unable to duplicate.

comment:5 Changed 7 years ago by edd

  • Status changed from closed to reopened
  • Resolution worksforme deleted

I can make this happen almost every time at  http://methvin.com/jquery/splitter/vsplitter.html -- Safari 3.0.4, Mac OS 10.5

I also have the problem where hide() doesn't work in a $(document).ready() context. I must use css({display: 'none'}) instead.

comment:6 Changed 7 years ago by edd

So, a workaround is to use $(window).bind("load", ...) instead of $(document).ready() on Safari.

I've still not figured out why this bug happens on some pages but not on others, alas.

comment:7 Changed 7 years ago by brandon

Can you verify this bug still exists with the latest SVN?

Changed 7 years ago by davidserduke

test case (slightly modified so can download)

comment:8 follow-up: ↓ 9 Changed 7 years ago by davidserduke

  • Milestone changed from 1.2.1 to 1.2.2

It seems to still happen in the current SVN. I modified the test case so you can download it. By editing the jquery_test.html file you can see there are two lines that are:

window.onload = (function () { $().ready(function ()

By commenting one and and one in, the problem shows itself.

comment:9 in reply to: ↑ 8 Changed 7 years ago by davidserduke

Let me try those two lines again:

window.onload = (function () {

$().ready(function () {

comment:10 follow-up: ↓ 11 Changed 7 years ago by brandon

I can also confirm this happens in Safari 3 win. However, I don't think we can rely on the window.onload event to always act as it currently does in Safari 3. The article you link to specifies that this "bug" is marked fixed in their bug tracker. Not sure what fixed means but I imagine it isn't want we want. The webkit nightlies support the DOMContentLoaded event but until then we will need to figure out anther hack for Safari.

comment:11 in reply to: ↑ 10 Changed 7 years ago by brandon

Replying to brandon:

I can also confirm this happens in Safari 3 win.

I meant to say: I can also confirm this happens in the latest Safari 3 win and mac.

Changed 7 years ago by davidserduke

simplified test case 2

Changed 7 years ago by davidserduke

simplified test case 2 data

comment:12 Changed 7 years ago by davidserduke

I'm not positive yet, this appears to be a problem with an external css file not being loaded and applied before the ready event fires. I'm guessing that is what causes this racing condition where it sometimes works and sometimes doesn't.

So I put together a simplified test case using php instead of a css file and a 1 second pause when loading it. There is a problem where the width is returning 'auto' at the ready event then '100px' at the onload event. I suspect this might be the problem. Opera is returning '0' at the ready event (but it works in the initial test case). FF and IE both return '100px' for both events.

Changed 7 years ago by davidserduke

patch

Changed 7 years ago by davidserduke

patch plus opera change

comment:13 Changed 7 years ago by davidserduke

  • Status changed from reopened to closed
  • Resolution set to fixed

Fixed in [4245]. There are many possible scenarios and edge cases to this kind of bug. The current fix seems fairly solid but until it gets tested in the beta we can't be sure. Please reopen the bug (if) with any problems you see ASAP.

comment:14 Changed 6 years ago by britter

  • Status changed from closed to reopened
  • Resolution fixed deleted

I am now noticing this problem again. Safari v3.2.1 windows. Issue is that Ready event fires before css files have loaded in safari. Simple / Easy work around is to declare your css files in the document head BEFORE the script files. If CSS files are after script files then the event fires before the css has loaded.

comment:15 Changed 6 years ago by dmethvin

I've updated the entry for .ready() to emphasize that css needs to all be loaded before script, especially on Safari.

 http://docs.jquery.com/Events/ready

comment:16 in reply to: ↑ description Changed 6 years ago by DG55

I can confirm this is a problem in Safari 3 for Mac, and Google Chrome (essentially all new Webkit based browsers).

Putting the CSS above the scripts does work, but it seems strange this hasn't been fixed yet - or is this a Webkit bug?

It seems fairly obvious that the whole point of the ready function is to start things at the point that all the essentials have loaded, so (if possible) a fix should be in order.

Changed 5 years ago by darkangel

comment:17 Changed 5 years ago by darkangel

I don't think this has anything to do with CSS. I've attached a very simple example that shows that setting designMode inside jQuery's ready() function fails in Safari Win 4.0 (530.17) and Chrome 2.0.172.30.

comment:19 Changed 5 years ago by john

  • Status changed from reopened to closed
  • Version changed from 1.2 to 1.3.2
  • Resolution set to fixed
  • Milestone changed from 1.2.2 to 1.4

An additional check was added to 1.4 to make sure that the body is available, at least, before the ready event will fire. This should resolve the issue.

comment:20 Changed 3 years ago by vitaliypodoba@…

I'm getting similar issue in Safari with images. Shouldn't ready() be called after browser fetched images on a page? At least It works fine in other browsers. But in Safari it only works on full page reload (Shift+Reload).

comment:21 Changed 3 years ago by ajpiano

No, ready is NOT fired after the browser downloads images on a page, ready is fired when the dom is manipulable but before the images have downloaded.

Note: See TracTickets for help on using tickets.