我剛剛制作了一個在 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>
uj5u.com熱心網友回復:
問題是您要宣告與引數同名的新變數。由于它們是用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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/487545.html
標籤:javascript 功能 获取 API
