问题 - 在 Windows Mobile 6.1 - IE Mobile6 上使用 Javascript 将动态 HTML 设置为 iFrame
(对不起,如果这不是发帖的正确论坛 - 我找不到任何与非本机编程相关以及与此主题相关的内容)
我正在尝试将动态 HTML 设置到网页上的 iFrame 中。我尝试了一些方法,但似乎都不起作用。我可以读取innerHTML,但似乎无法更新它。
// Able to read using
document.getElementById('iFrameIdentifier').innerHTML;
// On Desktop IE, this code works
document.getElementById('iFrameId').contentWindow.document.open();
document.getElementById('iFrameId').contentWindow.document.write(dynamicHTML);
document.getElementById('iFrameId').contentWindow.document.close();
理想情况下,相同的功能应该与 div 的工作方式相同,但它说“对象不支持此方法或属性”。
我也尝试过 document.getElementById('iFrameId').document.body.innerHTML。
这显然取代了 document.getElementById
我尝试了一些方法,但它们不起作用
- ('iFrameId').body.innerHTML
- document.frames[0].document.body.innerHTML
我的目的是拥有一个可以包含为其设置的动态 HTML 的容器元素,
直到现在我观察到由于 onClicks 或其他 JS 方法,在 div 上设置 innerHTML 所花费的时间越来越长。附加到动态 HTML 中的锚点和图像的 JS 方法或 HTML 似乎无法正确清理(内存泄漏?)
也正在讨论 - http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_26185526.html#a32779090
(excuse me if this is not the right forum to post - i couldn't find anything related to non-native programming and related to this topic)
I Am trying to set a dynamic HTML into an iFrame on the webpage. I have tried a couple of things but none of them seem to work. I m able to read the innerHTML but can't seem to update it.
// Able to read using
document.getElementById('iFrameIdentifier').innerHTML;
// On Desktop IE, this code works
document.getElementById('iFrameId').contentWindow.document.open();
document.getElementById('iFrameId').contentWindow.document.write(dynamicHTML);
document.getElementById('iFrameId').contentWindow.document.close();
Ideally the same function should work as how it works for div's but it says 'Object doesn't support this method or property".
I have also tried document.getElementById('iFrameId').document.body.innerHTML.
This apparently replaces the whole HTML of the page and not just the innerHTML.
I have tried out a couple of things and they didn't work
- document.getElementById('iFrameId').body.innerHTML
- document.frames[0].document.body.innerHTML
My purpose is to have a container element which can contain dynamic HTML that's set to it.
I've been using it well till now when I observed that the setting innerHTML on a div is taking increasing amount of time because of the onClicks or other JS methods that are attached to the anchors and images in the dynamic HTML. Appears the JS methods or the HTML is some how not getting cleaned up properly (memory leak?)
Also being discussed - http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_26185526.html#a32779090
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
欢迎来到 IEMobile!您所了解的有关 DOM 脚本的知识都不适用于此处。
不幸的是,跨 iframe 脚本在 IEMobile6-7 中似乎不可能。
frameelement.contentDocument
(标准 DOM 方法)不可用frameelement.contentWindow.document
(IE6-7 解决方法版本)不可用document
对象传递给window.parent
仅适用于框架,不是 iframe。在iframe
中,window.parent===window
。所以我能看到的唯一方法是:
document.cookie
在父级和子级之间进行通信:子文档只是一个脚本,它在轮询器上检查document.cookie
中的特定 cookie,并且当它发现这是来自父级的消息时,它可以编写一些 HTML 或其他内容。又慢又恶心。或者,的,可能。 IEMobile6-7(*) 在动态 HTML 中几乎无法使用。它让您领略到 Netscape 4 天前我们这些可怜的 git 编写脚本的感觉。
尽量避免创建和销毁大量节点和事件处理程序。尽可能保持页面静态,尽可能重用元素节点,并设置文本节点数据属性,而不是拆除所有内容并使用
createElement
或innerHTML
重新创建。如果您需要从 JavaScript 设置事件处理程序,请优先在 HTML 中使用事件存根 (onclick="return this._onclick()"
) 并写入_onclick
使用新的事件处理程序重新创建 HTML(或者只是尝试设置属性,这当然在 IEMobile 中不起作用)。尽可能避免长时间运行的单页。它仍然会崩溃,但希望需要更长的时间。
*:即 WinMo 6.1.4 之前的 IE 版本,其中它成为无限更好的 IEMobile8,营销为“Internet Explorer Mobile 6”(感谢 Microsoft)。
Welcome to IEMobile! Nothing you know about DOM scripting applies here.
Unfortunately, cross-iframe scripting does not appear to be possible in IEMobile6-7.
frameelement.contentDocument
(the standard DOM method) isn't availableframeelement.contentWindow.document
(the IE6-7 workaround version) isn't availablewindow.frames
array only works for frames, not iframesdocument
object to thewindow.parent
only works for frames, not iframes. In aniframe
,window.parent===window
.So the only ways forward I can see are:
document.cookie
to communicate between parent and child: the child document is just a script, that checks for a particular cookie indocument.cookie
on a poller, and when it's found that's a message from the parent, and it can write some HTML or whatever. Slow and nasty. Or,Yes, probably. IEMobile6-7(*) is close to unusable at dynamic HTML. It gives you a lovely flavour of what scripting used to be like for us poor gits back in the Netscape 4 days.
Try to avoid creating and destroying lots of nodes and event handlers. Keep the page as static as possible, re-using element nodes where possible and setting text node data properties in preference to tearing everything down and making anew with
createElement
orinnerHTML
. Use an event stub (onclick="return this._onclick()"
) in the HTML together with writing to_onclick
if you need to set event handlers from JavaScript, in preference to recreating the HTML with a new event handler (or just trying to set the property, which of course doesn't work in IEMobile). Avoid long-running single pages when you can.It'll still crash, but hopefully it'll take longer.
*: that is, the versions of IE present on WinMo before version 6.1.4, where it became the infinitely better IEMobile8, marketed as “Internet Explorer Mobile 6” (thank you Microsoft).
好的,我解决了之前遇到的问题,以及将 HTML 设置为 IEMobile 上的 iFrame 的更大问题。但我还有一个与双滚动条相关的 PIA - 我目前正在研究它。似乎有更多可怜的人面临着类似的问题——如果我也解决这个问题的话。我将在这里发布更新。
我最终是如何在 IEMobile 上写入 iFrame 的?
有 2 个 div,一个用于包装 iFrame,另一个用于在 iFrame 内写入。
这每次都会创建一个 iFrame,并且在加载时的 somefile.html 中有一个 InnerDiv.innerHTML ,它似乎不会泄漏内存。
在 somefile.html 中,将有一个 onLoad 方法,它将获取 HTML(下面解释我如何设法获取它)并执行
我如何设法在父 iFrame 和子 iFrame 之间传递 HTML
正如 @bobince 之前所解释的那样,必须依赖第三方服务(例如 cookie 或服务器)在父 iFrame 和子 iFrame 之间传递数据。
事实上,我使用 ActiveXControl 分别从父 iFrame 和子 iFrame 的 javascript 设置和获取数据。如果您必须为此引入 ActiveX 控件,我不建议您这样做。我无意中已经有了一个,我用它来获取动态 HTML。
如果您需要任何帮助,可以私信我 - Twitter @Swaroop
感谢 @bobince 的帮助。我将这个标记为答案,因为它说明了我为解决问题所做的事情。
Okay, I kinda resolved the issues that i was facing earlier and the bigger issue which was setting HTML to an iFrame on IEMobile. But i still have one more PIA which is related to double scollbars - which i am currently looking into. There seems to be more poor souls facing similar problem - if i fix that too. I will post an update here.
How did i finally write to iFrame on IEMobile?
Have 2 divs one to wrap the iFrame and the other to write inside an iFrame.
This creates an iFrame each time and in the somefile.html on load there is a InnerDiv.innerHTML which doesn't seem to leak the memory.
In the somefile.html there will be an onLoad method which will fetch the HTML (explained below on how i managed to get it) and do a
How did I manage to pass the HTML between parent and child iFrame
As well explained by @bobince earlier, one has to rely on 3rd party service like a cookie or a server to pass around the data between parent and the child iFrame.
I infact used an ActiveXControl to set and get data from the parent and child iFrame's javascript respectively. I won't recommend doing this if you have to introduce an ActiveX Control just for this. I accidentally already have one which I use to get the Dynamic HTML in the first place.
If you need any help you can DM me - Twitter @Swaroop
Thanks @bobince for your help. I am marking this one as an answer because it says what i did to fix the issue.