0

I am trying to add javascript to my page and use type="text/javascript" however when I include the following it just encloses it in script tags. How can I fix this? Also, how can I add the type="text/javascript to external files (see the second example)

drupal_add_js("$(document).ready(function() {
    // Infinite Ajax Scroll configuration
    jQuery.ias({
        container : '#itemcontainer',
        item: '.dealstable',
        pagination: '.nav',
        next: '.nav a',
    });
    });",'inline');

Here is the second example:

drupal_add_js('http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js','external');

UPDATE: As suggested by the comments I tried the following:

$js = "(function($) {
    jQuery.ias({
        container : '#itemcontainer',
        item: '.dealstable',
        pagination: '.nav',
        next: '.nav a',
    });
    };
})
(jQuery)";
drupal_add_js($js, array('type' => 'inline','scope'=>'footer'));

But that resulted in the following HTML:

<script>(function($) {
    jQuery.ias({
        container : '#itemcontainer',
        item: '.dealstable',
        pagination: '.nav',
        next: '.nav a',
    });
    };
})
(jQuery)</script>
  • 1
    text/javascript is the default type, though it appears Drupal core adds that by default anyway in drupal_get_js '#attributes' => array( 'type' => 'text/javascript', ), – David Thomas Apr 29 '14 at 7:10
2

you can use

$js = "(function($) {

    };
})
(jQuery)";
drupal_add_js($js, array('type' => 'inline','scope'=>'footer'));

OR

drupal_add_js("(function($) {
$(document).ready(function(){
    $('.mobile-navigation').click(function(){

     });
    });
})
(jQuery)","inline");

OR

drupal_add_js('sites/all/themes/dawn/js/skrollr.js', array('scope'=>'footer'));
drupal_add_js(drupal_get_path('module', 'module name') . '/js/name.js');
  • Please format your code as code. Also, a bit of description why it works and code by OP does not would be helpful. – Mołot Apr 29 '14 at 7:35
  • That didn't seem to work. I have updated my original edit to show the result. – John Apr 29 '14 at 8:14

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.