If you're using portal mode, make sure your widget/webpart is placed at the top of the page. For instance, when I create a master page I have a JavaScript webpart at the top of the template and at the bottom of the template. The one at the top loads JS inline at the top of the page and the bottom one loads JS inline at the bottom of the page.
Another example is Google Analytics. GA recommends placing their < script > tag as the last item before the closing < head > tag. To accomplish this I place the a HEAD HTML webpart as the very last webpart on the master page. This way it will still load in the < head > tag but all other items will ALWAYS load first and make this one last.