How to include javascript code before ?
If you had had that code directly in the page.tpl.php its normal, because your page is rendered in the same time as you try to add the JavaScript.

You should add your code in one file, at the root of your theme called template.php.