1

I'm calling the grandparent_func from a second-level nested iframe like so. This works in a non-Drupal environment, but am getting the following errors when running in drupal. (yes. I did remember to include the jquery file ;-)

parent.parent.grandparent_func( Date.now(), Date.now());

ERRORS:

For the following, I get a "TypeError: $ is not a function" error

<script>
    function grandparent_func(){        
        $("#fancy_regit_link").attr("href", "/my/actual/path");
        $("#fancy_regit_link").trigger('click');                

    }
</script>

So, I enclosed jquery like so to make it drupal-friendly and get: TypeError: parent.grandparent_func is not a function

<script>
(function($) {
    function grandparent_func(){        
        $("#fancy_regit_link").attr("href", "/my/actual/path");
        $("#fancy_regit_link").trigger('click');                

    }
})(jQuery);
</script>

For good measure, I double enclose it with jquery's ready function. TypeError: parent.grandparent_func is not a function

<script>
(function($) {
    $( document ).ready(function() {
        function grandparent_func(){        
            $("#fancy_regit_link").attr("href", "/my/actual/path");
            $("#fancy_regit_link").trigger('click');                

        }
    }); 
})(jQuery);
</script>

Any thoughts?

Thanks.

1 Answer 1

0

Introducing a closure around the whole function (as in your 2nd and 3rd examples) will take it out of the global scope, so it won't be available on the window, and subsequently the iframe's parent (which are the same thing).

What you're looking for is more like:

function grandparent_func() {        
  (function($) {
    $("#fancy_regit_link").attr("href", "/my/actual/path");
    $("#fancy_regit_link").trigger('click');                
  })(jQuery);
}

That makes sure grandparent_func is global, while still providing you the jQuery object.

2
  • This worked beautifully, Clive. Thanks. I have a feeling I'm going to be putting these all over the place now. This probably will create havoc when I take it back out of drupal. By chance, Do you know if there's a quick way to make that wrapper NOT a problem. I don't really understand what it's doing. Thanks again.
    – Jim Smith
    Jul 15, 2014 at 22:22
  • No need to worry, that's nothing to do with Drupal as such it's a simple anonymous function. It'll work the same outside of Drupal as it does inside, your code won't need to change
    – Clive
    Jul 15, 2014 at 22:26

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.