Safari:无法检索异步函数的参数



我刚刚做了一个函数,在Javascript页面上显示一个对话框,这个函数是异步的,因为它加载的对话框是一个PHP页面。然而,要显示消息,我需要传递参数。这个函数工作得很好,但在Safari上根本不起作用,当我显示参数时,在Safari上它返回"未定义",而在MS Edge上,它返回参数值。我尝试了另一个非异步函数,它显示参数很好。你认为Safari不处理异步函数的参数还是我做错了?非常感谢那些帮助我的人。(对不起,我的英语不好)。

功能:

async function openDialogInfo(title, message, button) {
alert(title + ' ' + message + ' ' + button);
var title = (typeof title !== 'undefined') ? encodeURI(title) : 'Information';
var message = (typeof message !== 'undefined') ? encodeURI(message) : 'Error retrieving message !';
var button = (typeof button !== 'undefined') ? encodeURI(button) : 'Ok';
if (title.length >= 3 && message.length >= 5 && button.length >= 3) {
if (screen.width >= 720) {
$("#out-popup-e").css({
'opacity': '0'
}).show();
} else {
$("#out-popup-e").show();
}
var params = `title=${title}&message=${message}&bouton=${button}`;
var resp = await fetch('../../../popup/popup_ecars_info.php', {
method: "POST",
body: params,
headers: {
"Content-type": 'application/x-www-form-urlencoded'
}
});
var out = await resp.text();
if (resp.status == '200') {
if (screen.width >= 720) {
$("#out-popup-e").animate({
'opacity': '1'
}, 400).html(out);
} else {
$("#out-popup-e").html(out);
$(".center-popup").css({
'bottom': '-700px'
}).animate({
'bottom': '0'
}, 400);
}
} else {
console.error('Error Message 2');
}
} else {
console.error('Error message 1');
}
}
<button class="action" onclick="openDialogInfo('HELLO WORLD', 'New message for testing (don't work on Safari)')">TEST</button>
<button class="no-action" onclick="autreTest('New Message (work)')">New test</button>

问题是您正在声明与参数名称相同的新变量。由于它们是用var声明的,因此声明被提升到函数的顶部,并且初始的未定义值覆盖了参数。

要么使用不同的变量,要么重新赋值,而不是重新声明。

async function openDialogInfo(title, message, button) {
alert(title + ' ' + message + ' ' + button);
title = (typeof title !== 'undefined') ? encodeURI(title) : 'Information';
message = (typeof message !== 'undefined') ? encodeURI(message) : 'Error retrieving message !';
button = (typeof button !== 'undefined') ? encodeURI(button) : 'Ok';
if (title.length >= 3 && message.length >= 5 && button.length >= 3) {
if (screen.width >= 720) {
$("#out-popup-e").css({
'opacity': '0'
}).show();
} else {
$("#out-popup-e").show();
}
var params = `title=${title}&message=${message}&bouton=${button}`;
var resp = await fetch('../../../popup/popup_ecars_info.php', {
method: "POST",
body: params,
headers: {
"Content-type": 'application/x-www-form-urlencoded'
}
});
var out = await resp.text();
if (resp.status == '200') {
if (screen.width >= 720) {
$("#out-popup-e").animate({
'opacity': '1'
}, 400).html(out);
} else {
$("#out-popup-e").html(out);
$(".center-popup").css({
'bottom': '-700px'
}).animate({
'bottom': '0'
}, 400);
}
} else {
console.error('Error Message 2');
}
} else {
console.error('Error message 1');
}
}
<button class="action" onclick="openDialogInfo('HELLO WORLD', 'New message for testing (don't work on Safari)')">TEST</button>
<button class="no-action" onclick="autreTest('New Message (work)')">New test</button>

最新更新