开发者

How can I insert a <SCRIPT> element into a WebView and run it?

I have a WebView in my app that has been loaded with an HTML string. The user makes a change which means a portion of the HTML now needs updating. Rather than reload the entire page, I want to update just a portion of it. So I do something like:

  1. Generate new HTML for portion of the page
  2. Create a DOMDocumentFragment from that HTML
  3. Replace the existing DOMElement with the new fragment

This works great, but not if the new HTML contains a <SCRIPT> tag. The script will be inserted in the DOM, but WebKit won't run it, leaving the page in a different state to if I'd reloaded it.

My understanding is WebKit doesn't run inserted scripts because that would potentially expose a security issue if a webpage could generate its own scripts and insert them. But this is my own app manipulating the page, not an untrusted website.

So what I'd like is some way to trigger WebKit running a script after inserting. Does anyone know how this can be done?


(Presently I am simulating this by loading the HTML into an offscreen webview,开发者_运维百科 letting it run the script, and then copying that across to the real webview. Works great for simple scripts, but fails on plenty more complicated ones, such as registering an event handler.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜