How to include javascript code before ? I want to integrate infolink code. Finally I copied this code just before print $closure in page.tpl.php file at the bottom to make it work:

&lt;script type="text/javascript"&gt;
   var infolink_pid = 326015;
   var infolink_wsid = 3;
&lt;/script&gt;
&lt;script type="text/javascript" src="http://resources.infolinks.com/js/infolinks_main.js"&gt;&lt;/script&gt;


But why this code does not work when placed in the same place?

&lt;?php
global $theme;
$js_fl_path=drupal_get_path('theme',$theme).'/infolink.js';

drupal_add_js($js_fl_path, 'theme', 'footer');
?&gt;


I'm using a subthemed Garland in sites/all/themes/ui_garland.
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.