0

I'm using Drupal and I have created three floating banners.

On the frontpage there is a block (block1) that displays one floating banner and after refresh the second one is appearing and for the third too. Like a wrote before these banners has a little X button to stop overflow. I've putted this script in a one of the banners and it's working great.

<script language="javascript">
function doexpand() {
    document.getElementById("block1").style.overflow = "visible";
}

function dolittle() {
    document.getElementById("block1").style.overflow = "hidden";
}    
</script>

The real problem is that in categories pages I have #block2 and in articles #block3. These block are displaying the same banners. The code over is working only for a one ID. In this case #block1. document.getElementById is not working for more ID's as I read from other topics.

I've tried with jQuery with two blocks idents like this:

(function ($) {

    function doexpand() {
     $("#block1,#block2").css("overflow","visible");
    }
    function dolittle() {
     $("#block1,#block2").css("overflow","hidden");
    }

    })(jQuery);

It's not working. The firebug/console displays: ReferenceError: doexpand is not defined.

I've tried with a single block too with jQuery like this:

(function ($) {

        function doexpand() {
         $("#block1").css("overflow","visible");
        }
        function dolittle() {
         $("#block1").css("overflow","hidden");
        }

        })(jQuery);

and it's displaying the same error.

Note: Drupal has a different wrapping and it's like this:

(function ($) {
        //your existing code
    })(jQuery);
raam86
  • 6,785
  • 2
  • 31
  • 46
user2519032
  • 819
  • 1
  • 18
  • 34

1 Answers1

3

Just define your function global, or you need to expose them global.

(function ($, window) {

        function doexpand() {
         $("#block1").css("overflow","visible");
        }
        function dolittle() {
         $("#block1").css("overflow","hidden");
        }

        // expose them global
        window.doexpand = doexpand;
        window.dolittle = dolittle;

})(jQuery, window);
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • Isn't making stuff global a bad practice? Wouldn't it be better to return those values and use them on window context? – raam86 Jun 26 '13 at 08:05
  • 1
    dude, just add action listeners to the banners - stop polluting your global namespace and the precious html with functions, variables, js calls and stuff. – Joshua Jun 26 '13 at 08:08