Use DOMContentLoaded event instead of window.onload.