Creating JS file on the fly
I would do this in this way:

function YOUR_MODULE_init(){
    drupal_add_js('path/you/want/to/use/to/include/javascript.js');
}

function YOUR_MODULE_menu() {
    $items = array();
    $items['path/you/want/to/use/to/include/javascript.js'] = array(
        'title' =&gt; 'Generated js',
        'type' =&gt; MENU_NORMAL_ITEM,
        'page callback' =&gt; 'your_function_name'
    );
    return $items;
}

function your_function_name() {
    $js = ... your code that composes js here ... ;
    print $js;
    exit;
}