通过JS加载XML文件,跨浏览器兼容
2024-09-03 20:37:22
引言
通过JS加载XML文件,跨多种浏览器兼容。
在Chrome中,没有load方法,需要特殊处理!
解决方案
部分代码
try //Internet Explorer { xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async=false; xmlDoc.load(file); } catch(e) { try //Firefox, Mozilla, Opera, etc. { xmlDoc=document.implementation.createDocument("","",null); xmlDoc.async=false; xmlDoc.load(file); } catch(e) { try //Google Chrome { var xmlhttp = new window.XMLHttpRequest(); xmlhttp.open("GET",file,false); xmlhttp.send(null); xmlDoc = xmlhttp.responseXML.documentElement; } catch(e) { error=e.message; } } }
完整代码
function parseXML(file){ var xmlDoc; try //Internet Explorer { xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async=false; xmlDoc.load(file); } catch(e) { try //Firefox, Mozilla, Opera, etc. { xmlDoc=document.implementation.createDocument("","",null); xmlDoc.async=false; xmlDoc.load(file); } catch(e) { try //Google Chrome { var xmlhttp = new window.XMLHttpRequest(); xmlhttp.open("GET",file,false); xmlhttp.send(null); xmlDoc = xmlhttp.responseXML.documentElement; } catch(e) { error=e.message; } } } return xmlDoc; }
最新文章
- 比较牛X的互联网公司都有哪些作死的行为
- [jquery]折叠指定条件的表格
- vc++ 判断文件或是文件夹是否存在,比较好的做法
- YHLMR007 服装PDA验货差异报表
- 用msbuild跑xunit单元测试
- Leetcode: Lexicographical Numbers
- 在js自定义函数中使用$(event.target)代替$(this)
- UVALive 4255 Guess
- Unicode和字符集小结
- Hadoop:Task process exit with nonzero status of 1 异常
- http keepalive and tcpkeepalive
- 《Getting Started with WebRTC》第一章 WebRTC介绍
- .NET/ASP.NETMVC Model元数据、HtmlHelper、自定义模板、模板的装饰者模式(一)
- CRS-2800 CRS-4000
- Linux下安装oracle jdk
- 推荐系统 LFM 算法的简单理解,感觉比大部分网上抄来抄去的文章好理解
- Java进阶篇设计模式之十一 ---- 策略模式和模板方法模式
- SSRS奇怪报错Could not update a list of fields for the quer.
- 作业20171026 alpha-2及alpha发布成绩
- PAT L2-022 重排链表