0

I have alot of javascript on a page, It can't be combined.

It's bugging me with the amount of "document.wait" and so forth functions on each of them, is there a better way of doing this, or am I stuck with it?

Goog'ling has revealed nothing.

Jared Allard
  • 726
  • 8
  • 33

2 Answers2

2

As said by others move all your scripts to the bottom of the page, right before </body>. It will solve many of your issues. But it will not tackle all the subtleties of browser-inconsistencies specially for old IE.

If you want to get a glimpse of how tricky is to provide such cross-browser implementation, take a look at the following popular question:

$(document).ready equivalent without jQuery

For instance do not lean against document.addEventListener as IE proposes its own proprietary document.attachEvent. And this is only the very first step.

Community
  • 1
  • 1
roland
  • 7,135
  • 6
  • 42
  • 60
1

The best practice is to always put script tags just before you close the body:

  <script src="path/to/my/script"></script>
  <!-- more scripts, etc -->
</body>

This way, everything else is definitely already loaded and no need to "wait". See this recommendation from google.

Otherwise, you can use:

window.addEventListener('load', function() {
  //your js here
});
m59
  • 41,346
  • 14
  • 107
  • 131