Ozh explains how to properly use JavaScript in your WordPress plugins. There are two key parts:
Use wp_enqueue_script() to load external libraries and standalone scripts, instead of generating your own script header tags.
On admin pages, use the admin_print_scripts-[mypage] action hook to register the code on only your plugin’s page, not every admin page.
Both of those [...]