0

screenshot

I want to make the same jquery menu in my custom drupal theme but I could not insert the inline script right before the body.

I made a separate JavaScript file, and added to my custom .info file, but this is not the right way to insert inline JavaScript, obviously.

screenshot

This is my info file.

screenshot

  • Where does the markup for the menus come from? – Andy Apr 24 '12 at 13:00
1

Not sure this helps but I had a similar problem before. I solved it by using the jQuery syntax instead of the $, like so:

jQuery(window).load(function() {
    jQuery('#a a').css({'background-position','-20px 55px'});
    // and so on
});

You could load your js file by calling the drupal_add_js() in the THEME_preprocess_page() hook in your template.php file.

  • Thx a lot @Krister Andersson, I made it with drupal_add_js(). drupal_add_js(drupal_get_path('theme', 'kei2').'/menuInlineJS.js', 'theme'); – wallace740 Apr 24 '12 at 13:06
  • @wallace740 Are you sure this is adding the code inline? – Andy Apr 24 '12 at 13:09
  • @Andy - To add it inline you could specify 'inline' as the second parameter to drupal_add_js() – Cyclonecode Apr 24 '12 at 14:32
  • @KristerAndersson I know - I was just surprised that the OP was asking specifically how to do it inline, and then posted a solution that wasn't! – Andy Apr 24 '12 at 14:43
0
drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });', 'inline');

Try with this refer here for more details : http://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_add_js/7

0

If you really need to insert the JavaScript code right after the <body> tag, you can add the following code right after the <body> tag.

<script type="text/javascript">
  (function ($) {
    $(function () {
      $('#a a') // ...
    });
  })(jQuery);
</script>

I suspect you are asking because a dialog box opens reporting a warning. That is because you should wrap the code using the jQuery library with (function ($) { /* Your code. */ })(jQuery); as it is done with any JavaScript file used from Drupal, such as batch.js.

(function ($) {

/**
 * Attaches the batch behavior to progress bars.
 */
Drupal.behaviors.batch = {
  attach: function (context, settings) {
    $('#progress', context).once('batch', function () {
      var holder = $(this);

      // Success: redirect to the summary.
      var updateCallback = function (progress, status, pb) {
        if (progress == 100) {
          pb.stopMonitoring();
          window.location = settings.batch.uri + '&op=finished';
        }
      };

      var errorCallback = function (pb) {
        holder.prepend($('<p class="error"></p>').html(settings.batch.errorMessage));
        $('#wait').hide();
      };

      var progress = new Drupal.progressBar('updateprogress', updateCallback, 'POST', errorCallback);
      progress.setProgress(-1, settings.batch.initMessage);
      holder.append(progress.element);
      progress.startMonitoring(settings.batch.uri + '&op=do', 10);
    });
  }
};

})(jQuery);

Putting the JavaScript code inlined right after the <body> tag, inlined inside the <head> tag, or in a file doesn't normally makes any difference, especially if all the code does is waiting for the DOM being loaded, and calls one or more jQuery functions, as in your case.

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.