This is perfectly possible.
If your JS code is not inside a custom widget you just have to include it in the header of the theme file.
If you require post-document-load you can add functions to mx.addOnLoad(). (If you overwrite window.onload the results are unpredictable).
As long as you don't override the Global variables 'mx', 'mendix' and 'dojo' you should be fine.
Or you can directly insert a javascript block into a specific form by using this widget: HTML/ JavaScript widget
Or (to add to the list here) create a widget for it and load it into your project. This lets you customize it in the Modeler instead of having to mess around in our theme/web map, but will require some more effort to create.
For more information: Custom Widget Tutorials.
You could insert your javascript code in the index of your theme.
You could insert your javascript code in the index of your theme.