0

I have the following code that gets the body height and writes it to a div that displays as a table-cell so the content can be vertically centered on any screen size on resize. I've tested this and it works fine.

$( window).resize(function() {
    bodyHeight = $('body').outerHeight();
    console.log('bodyHeight =' + bodyHeight);
    $('.table-cell').css('height', bodyHeight +'px');
});

Now if I try put it in Drupal using the code below. It doesn't work. jQuery is being called because I can run the script in Drupal but not when I specify to run it on resize.

jQuery(window).resize(function($) {
    bodyHeight = $('body').outerHeight();
    console.log('bodyHeight =' + bodyHeight);
    $('.table-cell').css('height', bodyHeight +'px');
});
1
  • have you written your code in Document.ready? Commented Sep 25, 2013 at 4:24

1 Answer 1

3

The first argument in the resize method is the event object, it's not jQuery.

jQuery(window).resize(function(e) {
    bodyHeight = $('body').outerHeight();
    console.log('bodyHeight =' + bodyHeight);
    $('.table-cell').height(bodyHeight);
});

You can also just use the .height() method instead of the .css() method.

If you're worried about conflicting $ variables, wrap it all in this:

jQuery(function($){
 // Your window resize here.
 // $ will reference jQuery within this scope
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks mate, your second suggestion, wrapping it in jQuery(function($) worked. I'll try out the .height() method too. It sounds cleaner than .css() Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.