3

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:doSomething('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. :-)

5
  • Do you have access to the HTML pages, i.e. are they third-party websites? Commented Aug 21, 2011 at 23:28
  • Can you not just use jquery's load? $('.selector').load('/path/to/file',callback); You can load full html files, with extra javascript, in there, and you do not have to worry about any extra serialization. Just write the file as if it was its own document. Your global JS libraries only need to be loaded on your main document, however. Commented Aug 24, 2011 at 0:37
  • Partially, I have access (there are HTML pages in my app that I construct in the app, imagine I want to update it with new big HTML content generated within the app without reloading the page), and partially, external websites are used (when I'm showing external content on their original page, where I also want to include some HTML and maybe a bigger JavaScript or two to handle user actions). ... jQuery's load loads data from a server, but I want to stick HTML generated in my iPad app into the page, not data I get from some server app. Commented Aug 24, 2011 at 7:05
  • 1
    I had the same problem. I found that it was faster and less painful to edit the HTML as a regular NSString and then call loadHTMLString:. The webview will scroll to the top, but i was able to fade it out, load HTML, and then fade it in again with the new content and it looked ok. Commented Aug 24, 2011 at 14:49
  • Nice idea, if nothing else orks I might do just that. Commented Aug 25, 2011 at 12:36

2 Answers 2

1
+50

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>
Sign up to request clarification or add additional context in comments.

1 Comment

This would mean that I'd need to write my generated HTML into a file to be able to load it via jQuery. It's not as nice as expected, but maybe it will work. Will try that out tomorrow.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.