4

I am loading a page into a WebView. The page has this little-test Javascript:

<script type="text/javascript">
function test(parametr)
{

  $('#testspan').html(parametr);

}

var bdu = (function(){
 return {
  secondtest: function(parametr) {

  $('#testspan').html(parametr);

  }
 }
})();
</script>

The problem is that I can't call the "secondtest" function from cocoa

this is perfectly work:

[[webview1 windowScriptObject] callWebScriptMethod:@"test" withArguments:arguments];

and this is not working:

[[webview1 windowScriptObject] callWebScriptMethod:@"bdu.secondtest" withArguments:arguments];

What would cause this problem and how do I fix it?

Thanks

1
  • I'll post this as comment instead of an answer: I don't know much about Javascript but it looks like the secondTest function is imbedded inside a block which itself is inside a function call. I don't see how anything external to script can resolve it as a callable function. (If this does work in Javascript, that language is an even bigger train wreck than I thought.) I would remove the function from its embedding and see if it works. If it does, then the embedding is wrong. Commented Feb 25, 2010 at 13:39

3 Answers 3

2

I was struggling with this for some time in Cordova MacOSX and here is the solution I found and works for me:

JavaScript:

CordovaBridgeUtil.isObject = function(obj) { return obj.constructor == Object; };

ObjectiveC:

WebScriptObject* bridgeUtil = [win evaluateWebScript:@"CordovaBridgeUtil"];
NSNumber* result = [bridgeUtil callWebScriptMethod:@"isObject" withArguments:[NSArray arrayWithObject:item]];
Sign up to request clarification or add additional context in comments.

Comments

1

"callWebScriptMethod" is meant to call a given method on a given webscript (i.e. javascript) object. On your second line you want to call the "secondTest" method on the javascript object named "bdu". The way to do this is to:

  1. Get the bdu object from your window object:

    WebScriptObject* bdu =[[webview1 windowScriptObject] valueForKey:@"bdu"];

  2. Call the "secondTest" method on the bdu object:

    [bdu callWebScriptMethod:@"secondtest" withArguments:arguments];

Comments

0

callWebscriptMethod:(NSString*)name withArguments:(NSArray*)args:

does not evaluate name as a Javascript expression. So it can't parse bdu.secondtest: it doesn't look up bdu first, then get the entry secondtest in it.

Instead, just use evaluateWebScript:.

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.