javascript - 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);
}
}
});
});
解决方案
您收到错误是因为showDialer
仅在传递给$(document).ready
in的回调范围内funcs.js
。为了在另一个脚本中使用它,您必须将其定义为全局函数,或者(更好地)将您的 2 个脚本合并到一个文件中,其中一个$(document).ready
包含所有内容。
推荐阅读
- r - R中的object.size vs lobstr::obj_size
- javascript - 在 Javascript StackTrace 时间线中是代码直接在布局前面的原因吗?
- python-3.x - 用于 GPS 坐标查找的 Pandas DataFrames
- c# - 登录控制台应用程序(带有 DI 的 .NET Core)
- java - 不再支持 java.endorsed.dirs=""。IntelliJ 2019.3
- gradle - 当构建脚本中的任务定义更改时,Gradle 是否应该使其任务缓存无效?
- java - 为什么这会给我一个错误(多线程或 ArrayList)?
- python - 使用字典映射后如何将数据框中的列更改为字符串
- r - 你如何循环 n 列并使用条件?
- python - 在 Python 中动态更改列名