[英]Extract XML content of an IFrame under IE9
I'm running with this issue since a week now 我已经有一个星期了
I wonder to do something very simple : 我想做一件非常简单的事情:
Extract the XML content contained within an iframe and consume it within the parent document ( in my case send the XML to an ActiveX for a signature ) 提取iframe中包含的XML内容并在父文档中使用它( 在我的情况下,将XML发送到ActiveX以获得签名 )
here is a url where to find the the mini project zip 这是在其中找到迷你项目zip的URL
my target browser is IE9/10 . 我的目标浏览器是IE9 / 10 。
Parent document : 上层文件 :
<!-- Main hero unit for a primary marketing message or call to action -->
<div class="hero-unit">
<h1>Parapheur 5.0</h1>
<div class="alert-block">Please use buttons for actions</div>
<div class="btn-group">
<a class="btn btn-success btn-mini" onclick="javascript:expandViewport('viewport1');">Expand viewport »</a>
<a class="btn btn-info btn-mini" onclick="javascript:collapseViewport('viewport1');">Collapse viewport »</a>
<a class="btn btn-primary btn-mini" onclick="javascript:extractXml('#viewport1');">Extract XML »</a>
</div>
<div class="alert-block"></div>
<iframe id="viewport1" src="xml/data.xml" width="1024px" height=""></iframe>
<div class="alert-block"></div>
</div>
<div id="content" class="alert-info"></div>
application.js : application.js :
function extractXml(id,content) {
console.log("Starting extraction of XML from within IFrame : " + id);
/*
var lastIndexOfSlash = document.getElementById(id).src.lastIndexOf("/");
var doc = document.getElementById(id).src.substring(lastIndexOfSlash);
$.get('xml/'+doc,function(xml){
console.log(xml);
});
*/
console.log($(id).contentDocument);}
在IE9下从iframe加载xml的唯一方法似乎是执行以下代码行。
document.getElementById("viewport1").contentWindow.document.firstChild.nextSibling.textContent
You can access the iframe xml in IE9 as below. 您可以按以下方式在IE9中访问iframe xml。
var frameIE = document.getElementById("yourIframeElementID");
var textXml = frameIE.contentWindow.document.firstChild.innerText;//Get the text
var regex = new RegExp('- <', 'g');
textXml = textXml.replace(regex, "<");
var xml = $.parseXML(textXml);//Use jquery to parse xml
xml = $(responseData);
//Now u can use this xml object to find xml nodes inside it using jquery...
I had to use this method when using <meta http-equiv="X-UA-Compatible" content="IE=8" />
. 使用
<meta http-equiv="X-UA-Compatible" content="IE=8" />
时,我不得不使用此方法。 In this case, IE9 works in compatibility mode and does not even allow access to window.frames.<your_frame_name>.document.XMLDocument
, and $("#iframeId").contents()
also does not work. 在这种情况下,IE9在兼容模式下工作,甚至不允许访问
window.frames.<your_frame_name>.document.XMLDocument
,并且$("#iframeId").contents()
也不起作用。
If you do not have <meta http-equiv="X-UA-Compatible"... />
tag, you can use the below code: 如果您没有
<meta http-equiv="X-UA-Compatible"... />
标记,则可以使用以下代码:
var contents = window.frames.your_frame_name.document.XMLDocument;
var xml = $(contents);
//use this 'xml' object to access its nodes using jquery.
But for all of above to work, your frame must be pointing to a resource within same origin or domain. 但是,要使以上所有功能正常工作,您的框架必须指向相同来源或域内的资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.