1

How can I pass a HTML string markup read from a file in objective C and pass it to a javascript function that takes a parameter ?

In my webViewDidFinishLoad I try to call like this

NSString *jsfile = [[NSBundle mainBundle] pathForResource:@"setcontent" ofType:@"js"];
NSString *js = [NSString stringWithContentsOfFile:jsfile encoding:NSUTF8StringEncoding error:&errDes];
[webView stringByEvaluatingJavaScriptFromString:js];
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"showHtml('%@');", filecontent]];

my javascript file is loaded with a function

function showHtml(param1)
{
    alert(param1);
    document.getElementById('section1').innerHTML = param1;
}

I tested this piece of code with explicit string values such as @"<p id=\"cid1\"> test </p>" and it works by setting the element contents. If I change this to @"<p id='cid1'> test </p>" i.e. with single quotes to the id attribute, it is not working. In order for the latter to work, I have to change the string formatter '%@' to \"%@\" in objective C code.

the above problem is not that serious but it is not working either way when reading the contents of a resource file containing multiple html tags that has to be set as an innerHTML to the #section1 to a div element.

Do I need to encode the contents somehow ? before calling the stringByEvaluatingJavaScriptFromString method ? Can anyone give me point me, what am I doing wrong ? if anyone know a better way to do this, please write.

Kind regards

1 Answer 1

0

there is a length limit here and you seem to run into it when passing a whole file to js

0

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.