1. 程式人生 > >JavaScript中執行function方法,並返回執行結果

JavaScript中執行function方法,並返回執行結果

以下程式碼實現JavaScript中執行MessageDialog方法成功後,執行回撥方法MessageDialogCallBack

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<title></title>
		<script type="text/javascript">
			//m_int_type:1 正確,2錯誤,3警告,4提醒
			function MessageDialog(m_int_icon, m_str_message, m_fn_callback) {

				var t_str_code_des;
				switch(m_int_icon) {
					case 1:
						t_str_code_des = "正確:" + m_str_message;
						break;
					case 2:
						t_str_code_des = "錯誤:" + m_str_message;
						break;
					case 3:
						t_str_code_des = "警告:" + m_str_message;
						break;
					case 4:
						t_str_code_des = "提醒:" + m_str_message;
						break;
					default:
						t_str_code_des = "提醒:" + m_str_message;
						break;
				}
					
				//如果有傳入回撥函式,則執行回撥函式
				if(typeof(m_fn_callback) != 'undefined' && typeof(m_fn_callback) == 'function') {
					m_fn_callback("Excute CallBack:"+t_str_code_des);
				} else {
					//如果沒有傳入回撥函式,則直接執行alert
					alert("Not CallBack"+t_str_code_des);
				}
			}
			
		//定義回撥方法
		function MessageDialogCallBack(response){
			alert(response);
		}
window.onload = function() {//測試:呼叫MessageDialog,傳入回撥方法名MessageDialogCallBackMessageDialog(4,"恭喜你,獲得Apple Mac一臺!",MessageDialogCallBack)}</script></head><body></body></html>