How do I add the "onload" attribute to a script tag? I need to add the onload event in jquery script tag as advagg does. I have done a bit of research, and I found that they are actually invoking hook_element_info_alter() and thereby add their hook_advagg_modify_js_pre_render() implementation into it. Then, they alter the child elements.

When I do the same in a hook_element_info_alter() implementation, type['script'] is not there.

Am I missing something?
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.