说明:此博客主要说明 通过javascript语言 取得特定格式的xml文件中的消息,从而对 项目的所有弹出消息进行统一管理
1:特定格式的xml文件 (消息内容处以根据用户需要进行扩展)
1 <?xml version="1.0" encoding="utf-8" ?> 2 <Messages> 3 <!--通知消息 I --> 4 <Message ID="I0001" Content="保存成功。"/> 5 6 <!--警告消息 W--> 7 <Message ID="W0001" Content="请输入{0}。"/> 8 9 <!--错误消息 E--> 10 <Message ID="E0001" Content="保存失败。"/> 11 12 <!--确认消息 Q--> 13 <Message ID="Q0001" Content="请确认是否删除{0}?"/> 14 </Messages>
2:通过 js代码取得 消息ID对应的消息内容
1 var XmlOperationApp = { 3 /** 4 * 取得消息内容 5 * @参数:xmlName 消息文件路径 6 *@参数 attributeID 消息编号 7 * @return:消息 8 */ 9 GetXmlContent: function (xmlName, attributeID) { 10 var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 11 xmlDoc.async = false; 12 xmlDoc.load(xmlName); 13 var items = xmlDoc.getElementsByTagName("Message"); 14 //循环出消息内容 15 for (var index = 0; index < items.length; index++) { 16 var item = items[index]; 17 if (item.attributes[0].name == "ID" 18 && item.attributes[0].value == attributeID) { 19 return item.attributes[1].value; 20 } 21 } 22 return ""; 23 24 } 25 }
3:前两步完成后,下来就可以通过消息ID通过统一的方法进行 取得消息内容
6 var MessageOperationApp = { 7 8 /** 9 * 取得消息内容 10 * @参数: {Object} messId 消息编号 11 * @参数: {Object} replaceChar 替换消息中的相关内容 12 * @return:消息 13 */ 14 GetMessage: function (messId, replaceChar) { 15 var messContent = XmlOperationApp.GetXmlContent('消息文件路径', messId); 16 //取得xml文件中的消息内容 17 if (replaceChar != "") { 18 messContent = messContent.replace("{0}", replaceChar); 19 } 20 return messContent; 21 }, 22 /** 23 * 提示画面消息 24 * @参数: {Object} messId 25 * @return:消息窗口 26 */ 27 ShowMessage: function (messId) { 28 alert(this.GetMessage(messId)); 29 }, 30 /** 31 * 提示画面消息 32 * @参数: {Object} messId * @参数: {Object} replaceChar 替换消息中的相关内容
33 * @return:消息窗口 34 */ 35 ShowMessage: function (messId, replaceChar) { 36 alert(this.GetMessage(messId, replaceChar)); 37 }, 38 /** 39 * 提示画面消息 40 * @参数: {Object} messContent 41 * @return:消息窗口 42 */ 43 ShowMessageContent: function (messContent) { 44 alert(messContent); 45 }, 46 /** 47 * 提示画面确认信息消息 48 * @参数: {Object} messId 49 * @return:返回true或者false 50 */ 51 ShowConfirm: function (messId) { 52 var msg = this.GetMessage(messId); 53 return window.confirm(msg); 54 }, 55 /** 56 * 提示画面确认信息消息 57 * @参数: {Object} messId 58 * @参数: {Object} replaceChar 替换内容 59 * @return:返回true或者false 60 */ 61 ShowConfirm: function (messId, replaceChar) { 62 var msg = this.GetMessage(messId, replaceChar); 63 return window.confirm(msg); 64 } 65 }
通过以上三步的完成 就可以 实现项目中 通过 javascript 脚本语言 对整个网站的弹出消息 进行统一管理
消息 包括 弹出消息 和 确认对话框消息 两种形式