我试图连接2个js文件,但似乎无法将变量传递给它。
language.js文件:
//SELECT LANGUAGE
var login_user, login_pass, login_page_loading_msg, login_button, login_register;
var dash_log_out, dash_loading_msg, dash_open_lifetime, dash_remind, dash_nothanks, dash_logout, dash_revenue, dash_value, dash_campaign, dash_status, dash_layer, dash_popdown, dash_leadorsale;
var stat_date, stat_view, stat_clicks;
var prog_choose, prog_info, prog_payperorder, prog_ordervolume, prog_countries, prog_conditions, prog_share, prog_url, prog_copytoclipboard, prog_copiedtoclipboard, prog_success, prog_ok;
var reg_title, reg_earn, reg_startearning, reg_participfree, reg_generalinfo, reg_language, reg_prefuser, reg_email, reg_details, reg_usertitle, reg_fn, reg_ln, reg_street, reg_postcode, reg_country, reg_tandc, reg_submit;
var menu_dash, menu_stat, menu_prog, menu_news;
$.getJSON("data/res/en.json", function(json) {
//ATTRIBUTE LANGUAGE
//LOGIN PAGE VARIABLES
login_user = json['login']['user'];
login_pass = json['login']['pass'];
login_page_loading_msg = json['login']['page_loading_msg'];
login_register = json['login']['register'];
login_button = json['login']['login'];
//DASHBOARD PAGE VARIABLES
dash_log_out = json['dashboard']['log_out'];
dash_loading_msg = json['dashboard']['loading_msg'];
dash_open_lifetime = json['dashboard']['open_lifetime'];
dash_remind = json['dashboard']['remind'];
dash_nothanks = json['dashboard']['nothanks'];
dash_logout = json['dashboard']['logout'];
dash_revenue = json['dashboard']['revenue'];
dash_value = json['dashboard']['value'];
dash_campaign = json['dashboard']['campaign'];
dash_status = json['dashboard']['status'];
dash_adklicks = json['dashboard']['adklicks'];
dash_layer = json['dashboard']['layer'];
dash_popdown = json['dashboard']['popdown'];
dash_leadorsale = json['dashboard']['leadorsale'];
$('#logOut').html(dash_log_out);
$('.loading').html(dash_loading_msg);
//STATISTICS PAGE VARIABLES
stat_date = json['statistics']['date'];
stat_view = json['statistics']['views'];
stat_clicks = json['statistics']['clicks'];
//PROGRAMS PAGE VARIABLES
prog_choose = json['program']['choose'];
prog_info = json['program']['info'];
prog_payperorder = json['program']['payperorder'];
prog_ordervolume = json['program']['ordervolume'];
prog_countries = json['program']['countries'];
prog_conditions = json['program']['conditions'];
prog_share = json['program']['share'];
prog_url = json['program']['url'];
prog_copytoclipboard = json['program']['copytoclipboard'];
prog_copiedtoclipboard = json['program']['copiedtoclipboard'];
prog_success = json['program']['success'];
prog_ok = json['program']['ok'];
//REGISTRATION PAGE VARIABLES
reg_title = json['registration']['title'];
reg_earn = json['registration']['earn'];
reg_startearning = json['registration']['startearning'];
reg_participfree = json['registration']['participfree'];
reg_generalinfo = json['registration']['generalinfo'];
reg_language = json['registration']['language'];
reg_prefuser = json['registration']['prefuser'];
reg_email = json['registration']['email'];
reg_details = json['registration']['details'];
reg_usertitle = json['registration']['usertitle'];
reg_fn = json['registration']['fn'];
reg_ln = json['registration']['ln'];
reg_street = json['registration']['street'];
reg_postcode = json['registration']['postcode'];
reg_country = json['registration']['country'];
reg_tandc = json['registration']['tandc'];
reg_submit = json['registration']['submit'];
//MENU VARIABLES
menu_dash = json['menu']['dash'];
menu_stat = json['menu']['stat'];
menu_prog = json['menu']['prog'];
menu_news = json['menu']['news'];
});
main.js文件:
[...]
console.log('login_button:'+ login_button);
[...]
这给了我:login_button:未定义的
如何从第一个文件中的函数中传递变量值?如果我在获取json值的函数外部分配它,那么它会正确传递,但函数内部的任何内容都会传递为未定义。
**
我是如何解决的:
**
language.js:
jQuery.extend({
getValues: function(url) {
var result = null;
$.ajax({
url: url,
type: 'get',
dataType: 'json',
async: false,
success: function(data) {
result = data;
}
});
return result;
}
});
var results = $.getValues(yourUrlString);
您可以在main.js中定义一个init函数,在language.js之前加载main.js。在language.jss中,您可以在设置完变量后调用init函数。我还建议您为全局变量命名名称空间。把它们放在一个巨大的物体里,也许有一个独特的名字。这样可以避免在全局对象中放入太多全局变量,并使其难以跟踪。
上述解决方案只能在init函数内部运行,因此,如果您需要在main.js中使用其他函数中的变量,那么可以这样做:
jQuery.extend({
getValues: function(url) {
var result = null;
$.ajax({
url: url,
type: 'get',
dataType: 'json',
async: false,
success: function(data) {
result = data;
}
});
return result;
}
});
var results = $.getValues(yourUrlString);