开发者

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 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?

EDIT: To explain a bit more, I have big snippets of HTML (containing also Javascript and stuff like <a href="javascript:do开发者_如何学编程Something('stupid')")> that I need to put in the DOM via JavaScript, after I load a website in an 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];. 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;.

Then I try to put that whole string into the DOM by doing this:

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. 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.

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. :-)


To load some file form inside UIWebView, do the following

<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 :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜