Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hi,
I’ve the following code inside a node.tpl.php. I have access to the variable my_height, but not the function my_readmore when HeadJS is enabled. What's going wrong? I can see the the code of my function wrapped inside the head.ready() function. Maybe I did it wrong.
drupal_add_js("
my_height = jQuery('#safe_value').height();
jQuery('#safe_value').hide();
jQuery('#safe_summary').show();
function my_readmore() {
jQuery('#readmore_block').animate({height:my_height+'px'});
jQuery('#safe_value').fadeIn();
jQuery('#safe_summary').css('position','absolute').fadeOut()
};", 'inline');
Comments
Comment #1
alexweber CreditAttribution: alexweber commentedWhen you say you have access to the variable my_height but not the function my_readmore, where and how are you trying to access them?
Comment #2
Wolfgang Reszel CreditAttribution: Wolfgang Reszel commentedIt's a onclick handler where I call the function.
I solved this problem by moving the function to the js file of the template. But I don't understand why.
Comment #3
alexweber CreditAttribution: alexweber commentedI can't say for sure that this was the cause of your problem, but something that causes a lot of headaches is variable and function scope and how it is affected by being called inside head.ready() as opposed to just being normally in the global namespace. I would imagine that to keep the variables global (their expected scope) it should be enough to simply ommit certain "var" declarations.
I still have to test this more to be sure but it might be something to work around and provide more detailed information in the documentation.