I've been using this code for quite some time, but am wondering if there's any improvements I can make to it. Basically it's just code that runs when the page loads or if the code is run after page load, it will run right away. Do you see any cross browser compatibility issues or any more efficient ways of writing this? ol()
is a predefined function to be run.
(document.readyState=='complete') ?
ol():(window.addEventListener&&window.addEventListener("load",ol,false)||
window.attachEvent&&window.attachEvent("onload",ol));
1 Answer 1
Just for dealing with ready
alone I use jQuery -- it does the cross-browser heavy lifting, is well-tested and maintained. Minified, it's only 32K, and included from a CDN, you get fast delivery that may already be cached in your user's browser.
And there is a lot more code than what you have above to deal with all of the browsers being ready.
There's some good coverage in Paul Irish's 10 Things I learned from the jQuery source screen-case .ready
coverage starts around 21:20. There's also some links to jQuery sources.
-
1\$\begingroup\$ using jQuery only for the ready event is to heavy.. \$\endgroup\$Wouter J– Wouter J2013年02月26日 16:02:03 +00:00Commented Feb 26, 2013 at 16:02
-
\$\begingroup\$ If you're not using a CDN, quite possibly. 32K is HUUUUGE. On my 300 baud modem it takes nearly a minute and a half to download. But the code above is nowhere near heavy enough. extract what you need from JQ, and credit it. I'd be surprised if you don't have other uses for it. \$\endgroup\$Michael Paulukonis– Michael Paulukonis2013年02月26日 16:25:08 +00:00Commented Feb 26, 2013 at 16:25
.ready()
\$\endgroup\$.ready
coverage starts around 21:20. There's also some links to jQuery sources. \$\endgroup\$