2
\$\begingroup\$

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));
Adam
5,2261 gold badge30 silver badges47 bronze badges
asked Feb 26, 2013 at 3:42
\$\endgroup\$
2
  • 2
    \$\begingroup\$ I suggest you take a look at jQuery's implementation of .ready() \$\endgroup\$ Commented Feb 26, 2013 at 8:28
  • 1
    \$\begingroup\$ There's some good coverage in Paul Irish's paulirish.com/2010/10-things-i-learned-from-the-jquery-source .ready coverage starts around 21:20. There's also some links to jQuery sources. \$\endgroup\$ Commented Feb 26, 2013 at 15:47

1 Answer 1

1
\$\begingroup\$

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.

answered Feb 26, 2013 at 15:52
\$\endgroup\$
2
  • 1
    \$\begingroup\$ using jQuery only for the ready event is to heavy.. \$\endgroup\$ Commented 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\$ Commented Feb 26, 2013 at 16:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.