1

I am trying to use drupal_add_html_head to add script elements to the head tag, however, this is important that these script tags are not self-closing because that is not the correct way to add them.

How do I do this:

$html5jquerymigrate = array(
    '#tag' => 'script',
    '#attributes' => array(
        'src' => libraries_get_path('jquery.migrate', true) . '/jquery-migrate-1.2.1.min.js',
    ),
    '#prefix' => '',
    '#suffix' => '',
);

drupal_add_html_head($html5jquerymigrate, 'html5jquerymigrate');

But instead of getting it printed out like this: <script src="/sites/all/libraries/jquery.migrate/jquery-migrate-1.2.1.min.js" />

How do I have it print out in the head like the following instead?

<script src="/sites/all/libraries/jquery.migrate/jquery-migrate-1.2.1.min.js"></script>

2 Answers 2

4

Ok, there was no 1 step solution to this problem. So here's what I did in case anyone else wants to know.

This is a 3 STEP solution to adding jQuery 1.9 and jQuery UI 1.10 functionality to Drupal 7. Not sure about other versions of Drupal.

1 - Needed to remove Drupals old jQuery library of version 1.4 and replace with version 1.9, so in my theme, I added the following function:

In template.php

function MYTHEME_js_alter(&$js) {

    $js['misc/jquery.js']['data'] = 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js';

    // Adding this ensures that the UI functionality gets added at all times!  Even when logged out of the site, since we have functionality on the site that requires this when logged out!
    if (!isset($js['misc/ui/jquery.ui.core.min.js']))
    {
        drupal_add_js('http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js');
    }
    else
        $js['misc/ui/jquery.ui.core.min.js']['data'] = 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js';
}

2 - Now that this is done, before we can really take advantage of Drupal with any other jquery library that is greater than 1.8 (which sucks), we need to add in the migrate plugin found here: http://blog.jquery.com/2013/05/08/jquery-migrate-1-2-1-released/

I have the Libraries Module installed, so I just put the JS in my Libraries folder called: jquery.migrate

3 - Than I added this to template.php file for my theme:

function MYTHEME_page_alter($page) {
    drupal_add_js(libraries_get_path('jquery.migrate') . '/jquery-migrate-1.2.1.min.js');
}

Now I can use all of the greatest and newest technologies in jQuery 1.9 and beyond while still being able to have Drupal 7 not crash on me! AMEN! HALLELUJAH!!!!

2

You can use drupal_add_js function instead. Read the API Documentation here: drupal_add_js
A Simple Example:

// This may do the work for you.
drupal_add_js('/sites/all/libraries/jquery.migrate/jquery-migrate-1.2.1.min.js');
// Using a CDN
drupal_add_js('http://code.jquery.com/jquery-latest.min.js', 'external');
5
  • So, and if you want to add a js library from a CDN?? Jan 6, 2014 at 21:53
  • Ok, this is almost what I want... BUT I need it to be inserted at the VERY TOP of the page... before any other script files. How to do this? I removed Drupals default jquery... But need to add my own so that it comes up before any other jquery files, how? Jan 6, 2014 at 21:58
  • 1
    To use CDN simply add a external parameter, see my edited answer.
    – rakibtg
    Jan 7, 2014 at 4:24
  • why do you add 'external' in the 2nd parameter? It doesn't seem to make a difference without that parameter altogether. Jan 7, 2014 at 7:30
  • 2
    it is a good practice and the important thing is you are telling drupal that it is a external JS file so drupal will not add any tokens or version ID, helpful for avoiding errors.
    – rakibtg
    Jan 7, 2014 at 7:53

Your Answer

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.