krux / postscribe

Asynchronously write javascript, even with document.write.
MIT License
986 stars 156 forks source link

External Scripts, General Query #468

Closed tomgallagher closed 6 years ago

tomgallagher commented 6 years ago

Hi

I've got the following code raising an error in an external js file run through Postscribe.

(function (window, document, $, Drupal) {
    "use strict";
    $(function () {
         // Re-attach js bindings.
         **Drupal.behaviors.insider_form.attach();** //this throws Uncaught TypeError: Cannot read property 'attach' of undefined
        }
    });
}(this, this.document, this.jQuery, this.Drupal));

The relevant property is defined further down the page in the same js file.

(function (window, document, $, Drupal) {
    "use strict";
    Drupal.behaviors.insider_form = {
        attach: function () {

            var $form = $('#insider-form');
            $('#edit-submit-ti-ajax').on('click', function(e) {
                var reg = new RegExp("(^|&)bd=([^&]*)(&|$)", "i");
                var param = window.location.search.substr(1).match(reg);
                var $adword;
                if (param != null) $adword = unescape(param[2]);
                var cookie = $.cookie('bd');

                if ($adword != null) {
                    $.cookie('bd', $adword, {expires : 30});
                    $('input[name=ad_word_ti]').val($adword);
                } else {
                    if (cookie != null && cookie != '') {
                        $('input[name=ad_word_ti]').val(cookie);
                    }
                }
            });
       };

}(this, this.document, this.jQuery, this.Drupal));

I'm not looking for any help with this code, I was just wondering if anyone could see something obvious, with their knowledge of Postscribe, why this would be throwing an error.

Any help much appreciated.

Tom