简体   繁体   English

代码片段使用复杂的 JavaScript 在 UIWebView 中插入 HTML?

[英]Code Snippet to use complex JavaScript to insert HTML inside UIWebView?

I have to do some complex work with a UIWebView and Javascript and always get problems when I try to insert complex Javascripts involving JQuery and HTML Snippets that contain JavaScript themselves. I have to do some complex work with a UIWebView and Javascript and always get problems when I try to insert complex Javascripts involving JQuery and HTML Snippets that contain JavaScript themselves. I never seem to have enough quote and single quote signs and putting in script elements is kinda painful.我似乎从来没有足够的引号和单引号符号,并且放入脚本元素有点痛苦。

I there a magic Objective-C snippet, or an easier copy-pasteable way of using more complex scripts that you use for such tasks?我有一个神奇的 Objective-C 片段,还是一种更简单的复制粘贴方式来使用用于此类任务的更复杂的脚本?

EDIT: To explain a bit more, I have big snippets of HTML (containing also Javascript and stuff like <a href="javascript:doSomething('stupid')")> that I need to put in the DOM via JavaScript, after I load a website in an UIWebView.编辑:再解释一下,我有 HTML 的大片段(还包含 Javascript 和<a href="javascript:doSomething('stupid')")>之类的东西,我需要通过 Z686155AF9A67EDDFE6 之后放入 DOM在 UIWebView 中加载网站。

My approach now is to put the HTML stuff into files, rewrite them so that I for example have 'stupid' in a JS-variable so I don't need additional single quotes, put placeholders into place like ###placeholder1###, load that file into an NSString and replace those placeholders by dynamic data generated in my app via html = [html stringByReplacingOccurrencesOfString:@"###placeholder1###" withString:dynamicStuffHtml];我现在的方法是将 HTML 的东西放入文件中,重写它们,以便例如我在 JS 变量中有“愚蠢”,所以我不需要额外的单引号,将占位符放置到位,如 ###placeholder1### ,将该文件加载到 NSString 中,并用我的应用程序中通过html = [html stringByReplacingOccurrencesOfString:@"###placeholder1###" withString:dynamicStuffHtml]; . . The dynamic data before contains no single quotes, other than in the text-content, for which I replace the single quotes with the HTML-encoded &#39;之前的动态数据不包含单引号,除了在文本内容中,我将单引号替换为 HTML 编码的&#39; . .

Then I try to put that whole string into the DOM by doing this:然后我尝试通过这样做将整个字符串放入 DOM:

NSString* javascript = [NSString stringWithFormat:@"%@%@%@",@"jQuery('.focus_box').first().before('",html,@"');"];
[webView stringByEvaluatingJavaScriptFromString:javascript];

This works for easy and short examples (for example when I put in short paragraphs or simple HTML), but for the full-blown approximately 50-60 lines HTML snippet, it just doesn't work.这适用于简单和简短的示例(例如,当我放入短段落或简单的 HTML 时),但对于大约 50-60 行的完整 HTML 片段,它只是不起作用。 Additionally, all that editing, replacing, patching and stuffing made me think that maybe someone using UIWebViews and JavaScript has a better solution that is known to work.此外,所有这些编辑、替换、修补和填充让我认为,也许有人使用 UIWebViews 和 JavaScript 有一个更好的解决方案,并且已知可以工作。

EDIT AGAIN: I started a bounty, because I cannot believe that there's so few usages of UIWebView that nobody has an answer to this, or already has a big private Tools-class to handle such more complex JavaScript interaction where big chunks of HTML can be included without a reload.再次编辑:我开始了赏金,因为我不敢相信 UIWebView 的用法如此之少,以至于没有人对此有答案,或者已经有一个大型私有工具类来处理如此更复杂的 JavaScript 交互,其中 Z744DB33A 的大块可以 beFAAC4AD5FCA2E037A包括没有重新加载。 :-) :-)

To load some file form inside UIWebView, do the following要在 UIWebView 中加载一些文件形式,请执行以下操作

<script language="javascript" type="text/javascript">
var bundlePath = "here_goes_full_path_to_your_bundle"; 
// It should be something like file:///var/.../YouApp.app 
// Set it from your code

jQuery.get(bundlePath + "/someFile.html", function(data) {alert(data);});
</script>

I created some websites for internal stuff, that use this technique quite excessively.我为内部资料创建了一些网站,这些网站过度使用了这种技术。 The biggest problem I had is getting the encapsulated quotes right because all your quotes have to be masked (with )我遇到的最大问题是正确获取封装的引号,因为您的所有引号都必须被屏蔽(使用 )

Here is a little snippet:这是一个小片段:

code += "<table onclick='gbyid(\"suchfeld " + parametername + "\").value = \"\", " + parametername + " = \"\"'><tr><td>Delete Date</td></tr></table>"

I don't know, if this helps, but 'it just doesn't work' is no big hint:)我不知道,这是否有帮助,但“它只是不起作用”并不是什么大提示:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM