0

I'm new to PhoneGap/Cordova, and would like to call some javascript outside of the CDVPlugin interface.... In code that isn't part of a plugin.

Is this possible? If I could get a reference to the webview that PhoneGap uses, I should be able to call something like:

[webView stringByEvaluatingJavaScriptFromString:@"myJSFunction()"];

or

[super writeJavascript::@"myJSFunction()"];

1 Answer 1

0

You can do this for ios by getting a reference to the WebView from the AppDelegate:

+(void) fireJavascript:(NSString*) javascript
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    [appDelegate fireJavascript:javascript];
}

and in your AppDelegate, define a method like so:

- (void) fireJavascript:(NSString *)javascript
{
    NSLog(@"About to fire js:%@", javascript);
    [self.viewController.webView stringByEvaluatingJavaScriptFromString:javascript];
}

Note that you can get a reference for the app delegate in your app using info from this question on StackOverflow.

Sign up to request clarification or add additional context in comments.

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.