遇到以下代码问题。我在网站上进行了一次搜索,然后拿出了接近但没有雪茄的东西。
我希望点击附加到.callapp的项目时执行以下操作:
我不知道如何执行我传递给“requesthandler”的匿名函数。我需要这个匿名函数来执行带有我传入的参数的“loadURL”函数,这将会激活另一个与此帖无关的事件链。
谢谢!
var url = "";
var tab = "";
var app = "base";
var out = "";
$(document).ready(function() {
$(".callapp").click(function(e) {
e.preventDefault();
$('#wxreturn').html("<div id='xtools'></div><div id='xerror'></div><div id='xdapps'></div>");
hideContent(1, 1);
$('#xdapps').load( getURL("base"), function(resp, stat, xhr) {
requesthandler(iStat, iXHR, 0, 0, 0, 0, function() {
loadURL("tools", "xtools", 1, 1, 1, 1);
});
});
return 0;
});
});
// piece together a url
function getURL(xapp) {
// url to return
var url = null;
// global tab must not be empty
if (tab.length) {
// check if app is defined
if (!xapp.length) {
// app is either the global or base
xapp = app | "base";
} else {
// set the global
if (!(xapp == "tools") && !(xapp == "options")) app = xapp;
}
// set the url to return
url = "/" + tab.toLowerCase() + "/" + xapp.toLowerCase() + "?_=" + Math.random();
} else {
// undefined functionality error
alert("Invalid getURL...Tab Missing");
}
// return the url
return url;
}
// load a url
function loadURL(xapp, target, showApp, showOpt, showTools, clearTools) {
// defaults
showApp = showApp | 0;
showOpt = showOpt | 0;
showTools = showTools | 0;
clearTools = clearTools | 0;
// do only if app and target are defined
if (!(xapp == undefined) && !(target == undefined)) {
// set target
if (!(target.contains("#"))) target = "#" + target;
// get url string
var url = getURL(xapp);
// check if null
if (!(url == null)) {
// commence with load
$(target).load(url, function(resp, stat, xhr) {
// call back with the request handler
requesthandler(stat, xhr, showApp, showOpt, showTools, clearTools);
});
}
} else {
// undefined functionality error
alert("Invalid LoadURL...Missing App...Target");
}
}
// request handler
function requesthandler(stat, xhr, showApp, showOpt, showTools, clearTools, onSuccess) {
// defaults
showApp = showApp | 0;
showOpt = showOpt | 0;
showTools = showTools | 0;
clearTools = clearTools | 0;
onSuccess = onSuccess | null;
// check for status
if (stat == "success") {
// execute
if (!(onSuccess == null)) {
// perform function
(onSuccess());
}
} else {
if (xhr.status == 401) {
// throw session expired
sessionTimeOut();
} else if (xhr.status == 403) {
// throw authorization failure
failedAuthorize();
} else {
// throw application request failure
failedAPPRequest(clearTools);
}
}
}
答案 0 :(得分:3)
onSuccess = onSuccess | null;
您需要在这里使用两个|
,而不是一个。
showApp = showApp || 0;
showOpt = showOpt || 0;
showTools = showTools || 0;
clearTools = clearTools || 0;
onSuccess = onSuccess || null;
为了安全起见,我在运行之前检查onSuccess
是否是一个函数(不仅仅是它不是null):
if (typeof onSuccess === 'function') {
// perform function
onSuccess(); // the extra "()" aren't needed here
}
如果您这样做,则不需要onSuccess || null
。