首页 > 解决方案 > ReferenceError: functionxyz is not defined

问题描述

I am having an error that kinda makes me upset atm. I have three script files, one jQuery, another one including all my functions and the last one is a file where I use one of the function in the functions-script-file.

    <!-- SCRIPTS -->
    <script type="text/javascript" src="http://localhost/assets/web/js/jquery/de.jq.311.js"></script>
    <script type="text/javascript" src="http://localhost/assets/web/js/func.js"></script>
    <script type="text/javascript" src="http://localhost/assets/web/js/sign/signup.js"></script>

Signup.js now tells me that a certain function that actually is defined in func.js is not defined, even tho they are all in the correct order and I have checked it twice, the function is spelled the right way. I also have tried to put the functions directly in the head tag above the signup.js and that worked just fine.

func.js:

$(document).ready(function(){

    // VARS
    var body = $('body');


    // FUNCTIONS
    var dialerTimeout;
    function showDialer(text) {
        var t  = text;
        clearTimeout(dialerTimeout);
        var rd = $('response-dialer');
        rd.find('.inr p').html(text);
        rd.css('bottom', '12px');

        dialerTimeout = setTimeout(function(){ rd.removeAttr('style'); }, 3000);
    }
});

signup.js:

$(document).ready(function(){

    $(document).on('click', '[data-action="signup"]', function(){

        var mail  = $('input[name="mail"]').val();
        var pass  = $('input[name="password"]').val();
        var pass2 = $('input[name="password2"]').val();
        var agb   = $('input[name="agb"]').val();
        var rd    = $('response-dialer');
        var lc    = $('login-container');

        console.log($('[data-form="signup"]').serialize());

        if(mail === '' || pass === '' || pass2 === '') {
            showDialer('Bitte fülle alle Felder aus!');
        } else if(pass !== pass2) {
            showDialer('Ihre Passwörter stimmen nicht überein!');
        } else if(grecaptcha && grecaptcha.getResponse().length === 0) {
            showDialer('Der Captcha-Code ist falsch!');
        } else if(!($('#agb').is(":checked"))) {
            showDialer('Bitte lese und akzeptiere unsere AGB!');
        } else {

            if(!validateEmail(mail)) {
                showDialer('Ihre E-Mail hat ein falsches Format. Bitte nutze name@host.endung');
            } else {

                addOverlay(lc);

            }

        }

    });
});

标签: javascriptjqueryhtml

解决方案


您收到错误是因为showDialer仅在传递给$(document).readyin的回调范围内funcs.js。为了在另一个脚本中使用它,您必须将其定义为全局函数,或者(更好地)将您的 2 个脚本合并到一个文件中,其中一个$(document).ready包含所有内容。


推荐阅读