0

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?

New contributor
Mainak Ray is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • What is the reason you can't use drupal behviours for the onload events? sqndr.github.io/d8-theming-guide/javascript/behaviors.html – Rick B Mar 24 at 15:10
  • I want add the onload event only to jquery library and the drupal js asbecause if I add the defer attribute to them or move them to footer the whole page got collapsed – Mainak Ray Mar 25 at 5:51
  • Assuming you're using Drupal 8, And your page is collapsing, so I think it is about a theme. you should then declare your script in libraries as head. I'll try to explain it in the answser – Rick B Mar 25 at 8:57
  • Sorry I am using drupal 7 . Is there any way I can do it? – Mainak Ray Mar 25 at 12:38
0

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(&$var) {
      $path = $GLOBALS['base_url'] . '/' . path_to_theme() . '/js/yourscript-min.js';
      $script = array(
        '#tag' => 'script',
        '#attributes' => array(
            'type' => 'text/javascript',
            'src' => $path
        ),
        '#value' => '',
       );
       drupal_add_html_head($script, 'script');  
    }

or if you want it loaded in the page:

function theme_preprocess_html(&$var) {
      $script = array(
        '#tag' => 'script',
        '#attributes' => array('type' => 'text/javascript'),
        '#value' => '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.

  • Thanks fro your suggestion . I am using drupal 7 – Mainak Ray Mar 25 at 12:39
  • I've updated the answer for drupal 7 – Rick B Mar 25 at 14:09
  • Sorry I did not asked , that , My Question is how to override hook_advagg_modify_js_pre_render like advagg does and how they implemented advagg_get_js – Mainak Ray Mar 26 at 13:33

Your Answer

Mainak Ray is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.