Use clientScript->registerScript to register the javascript otherwise it gives problems on pages where other javascript files are included.