How do I add the "onload" attribute to a script tag?
If you want to make sure you're javascript is loaded in the header:

For drupal 7

/sites/default/themes/yourtheme/template.php

function theme_preprocess_html(&amp;$var) {
      $path = $GLOBALS['base_url'] . '/' . path_to_theme() . '/js/yourscript-min.js';
      $script = array(
        '#tag' =&gt; 'script',
        '#attributes' =&gt; array(
            'type' =&gt; 'text/javascript',
            'src' =&gt; $path
        ),
        '#value' =&gt; '',
       );
       drupal_add_html_head($script, 'script');  
    }


or if you want it loaded in the page:

function theme_preprocess_html(&amp;$var) {
      $script = array(
        '#tag' =&gt; 'script',
        '#attributes' =&gt; array('type' =&gt; 'text/javascript'),
        '#value' =&gt; 'Add your javascript here',
       );
       drupal_add_html_head($script, 'script');  
    }


For drupal 8

In the yourtheme.libraries.yml you need to create a library like so:

yourtheme.libraries.yml

library-name:
  header: true
  js:
    js/yourSrctipForTheHeader.js: {}


Also in the theme.info.yml you need to add this library, then it will automatically comes in head tag and will be available before the page load, if you remove header:true the script will be loaded after the page load.

theme.info.yml

libraries:
  - yourtheme/library-name


Alternativly, you can make use of this module: 
https://www.drupal.org/project/header_and_footer_scripts and put the scipts in manually in the position that you want. But to be honest, i think the first solution is the more elegant way.