Posts Tagged ‘safari’

jQuery document ready vs Safari

Thursday, May 7th, 2009

When I had been fiddling with the tweenmenu I had to realize that sometimes offsetLeft or position().left doesn’t work in Safari/Chrome – like many others (Paul Carey, Dave Methwin, Yereth) I came to the conclusion that the problem is with jQuery document.ready though it is very very rare (assuming one moved all the css before scripts!!), but sometimes it does happen (like others noted in the bugtracker).

After some experimenting and deleting 99% of this post :) – adding an empty style tag dinamically magically fixes the problem:

(function(){
if (!/WebKit/i.test(navigator.userAgent)) return;
var el = document.createElement("style");
el.type = "text/css";
el.media = "screen, projection";
document.getElementsByTagName("head")[0].appendChild(el);
el.appendChild(document.createTextNode("_safari {}"));
})();