将 iframe 的内容类型更改为 xml 以显示 xml 并通过 DOM 操作它
我想使用 JavaScript 执行以下操作:
- 构建 XML 文件
- 在 iframe 中显示它
- 通过 DOM 操作内容
我正在构建一个 XML 编辑器,但在 iFrame 中显示 xml 时遇到问题。
这是我目前正在使用的代码。
function previewContent(what){//changes the content of an iFrame
var tsite = document.getElementById('xmlinside').contentDocument;
tsite.open();
tsite.contentType('text/xml');
tsite.writeln(what);
tsite.close();
}
function makeXML(){
var tester = '<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\
<bookstore>test</bookstore>';
previewContent(tester);
}
如果没有 tsite.contentType('text/xml')
,iframe 只会显示 test
。
当我检查源代码时,它仍然是 HTML 格式,并添加了行
添加了 xml
版本作为注释。
我希望它像没有样式表的浏览器一样显示。我知道如果没有与之关联的格式,Safari 会将其显示为原始 xml 文件。这将是完美的。是内容类型搞乱了吗?这就是为什么我尝试更改它,但这是错误的代码。任何帮助都会很棒!
I would like to use JavaScript to do the following:
- Build an XML file
- display it in an iframe
- manipulate the content through DOM
I'm building an XML editor and am having trouble displaying the xml in an iFrame.
this is the code that I'm using at the moment.
function previewContent(what){//changes the content of an iFrame
var tsite = document.getElementById('xmlinside').contentDocument;
tsite.open();
tsite.contentType('text/xml');
tsite.writeln(what);
tsite.close();
}
function makeXML(){
var tester = '<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\
<bookstore>test</bookstore>';
previewContent(tester);
}
without the tsite.contentType('text/xml')
the iframe simply displays test
.
when I check the source it's still in HTML with the added line <bookstore>test</bookstore>
added and the xml
version as a comment.
I would like it to display like a browser without a stylesheet. I know Safari displays it as a raw xml file if there is no formatting associated with it. This would be perfect. Is it the content type that is messing up? That's why I tried changing it but this is the wrong code. Any help would be great!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
需要记住的一些事情...
1.) 如果它是通过 AJAX 加载的内容,您将需要使用 application/xml 媒体类型/mime,并且第一个元素必须具有名称空间。 2.
) 永远不要使用innerHTML(和框架,因为它们严重依赖innerHTML),尤其是永远不要将它与加载AJAX加载的内容结合使用,微软专有的方法不能正确注册DOM,所以你会看到代码是在那里,但它并不真正在那里使您的代码在那时完全不可靠。如果使用 JavaScript,请使用 importNode 方法加载 AJAX 加载的内容(您可以将其导入到 div 元素中,而不是使用 iframe)。
3.) 如果 iframe 不是从同一个域加载的,您将无法(也不应该)访问 iframe 的 (X)HTML,因此,如果它是从第三方网站加载的,请忘记它,否则您可以说操作文本并发现他们的信息(电话、电子邮件等,如果他们登录了某些内容)。
如果您满足这三个条件,您应该能够像不涉及 iframe 或 AJAX 一样使用代码。
A few things to keep in mind...
1.) If it's content loaded via AJAX you will need to use the application/xml media type/mime and the first element has to have a namespace...
2.) Never use innerHTML (and frameworks as they rely heavily on innerHTML) and most especially never use it in conjunction with loading AJAX loaded content, the proprietary Microsoft method does not correctly register the DOM so you sort of see that the code is there but it's not really there thus making your code wholly unreliable at that point. If using JavaScript use the importNode method to load AJAX loaded content (instead of using an iframe, you can import it to a division element instead).
3.) You won't (and shouldn't) be able to access (X)HTML of an iframe if it's not loaded from the same domain so if it's being loaded from a third party website forget about it otherwise you could say manipulate the text and discover their information (phone, email, etc if they're signed in to something).
If you meet those three conditions you should be able to work with code as if there were no iframes or AJAX involved to begin with.