我正在尝试连接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:undefined
如何从第一个文件中的函数中传递变量值?如果我将它分配给获取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);
答案 0 :(得分:1)
你可以在main.js中定义一个init函数,在language.js之前加载你的main.js。在language.js中,在完成变量设置后调用init函数。 我还建议您命名全局变量。将它们全部放在一个具有唯一名称的巨型物体中。这样可以避免将过多的全局变量放入全局对象中,并使其难以跟踪。
答案 1 :(得分:0)
上述解决方案只能在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);