Calling javascript functions from android activity - javascript

Is it possible to call a javascript function from java code in Android. Consider the javascript function in a .js file.
function calcSum(firstNumber, secondNumber){
return (firstNumber+secondNumber);
}
I want to call this function from my java code and show the result in TextView. I don't want to use WebView.

If you dont want to use web view you will need a nother kind of javascript interpreter. For example Rhino:
http://www.mozilla.org/rhino
http://en.wikipedia.org/wiki/Rhino_(JavaScript_engine)

Related

How can I call a Javascript function from a website? [duplicate]

This question already has an answer here:
How to call external javascript function from jslib plugin Unity webgl
(1 answer)
Closed 1 year ago.
I'm working on a program that will be used by different users and they will individually change the content of the js file to match with their expectation.
On the web I found many tutorials on how to call JS function in unity WEBGL but the Js file was built with the unity project.
I wanted a way to call a JS function which is hosted by the website (incluing it in the index.html of unity WEBGL but without building it).
What you are trying to do is possible. But it is a bit of a hack.
a quick google search turned up this article, from the unity documentation
But it might not be clear how to do it. so here is an example:
example:
Let us say you want to call a js function called foo().
.jslib
The first thing to do is create a file with a .jslib file extension in a folder named "Plugins".
this file tells unity that the function exists and is a just js
mergeInto(LibraryManager.library, {
foo_js: function () {
foo()
},
});
Here I declare a function called foo_js and tell unity that it exists.
This function consists of js, and in this case, all it does is call the js function foo().
Note: the name foo_js is arbitrary, and it can be called anything, Including just foo. but it is the same name as you will be using in c#. so I would recommend making it clear that it is not a native c# function
c#
now in a c# script, you start by declaring the function, which is done by adding
[DllImport("__Internal")]
private static extern void foo_js();
to a class and add
using System.Runtime.InteropServices;
to the top of the file
you can now call the function as foo_js() in your c# script
after built
after you have built the unity project, you need to add a js script. To "index.html" that has a function called foo() for unity to call.
And it should work, for more detail read the article from the beginning.
Edit:
Something I forgot to mention is that it is not going to work in the editor, as the js function is not defined, so you need to export the project and define the function on the website
it should be possible to detect that the game is running in the editor and use a default behaviour instead of the js function, but I am not sure how right now

source code for the alert() function

What is the source code for the default alert() function in Javascript(method of the window object)? I am trying to write an alert function myself so I would really like to get a peek at the original function code.It is so hard to google it.
The alert() function, like a number of other standard functions, is a part of the browser's Javascript runtime. It cannot be replicated with Javascript code (other than by calling alert()), and there is no platform-independent Javascript source code to the function.
It's a compiled function, probably different for every browser, so there is no source JS code for it.
You've noticed when you call it how it takes over the whole browser - it's not a part of the webpage like a popup or something; so if you're trying to achieve something like that (control at a lower level of the browser), there's no way to do it from JS. If you only need a popup, there are tons of online resources about how it do it.
The source code for alert() is... alert()
It is a primitive Word. If you want details, they are yet in another language.
Interpreted directly by the browser.

Get output from JavaScript function in C++

Hy,
I'm working at a project that must call from C++ a custom function made in JavaScript. I'm able to run the function
The project should work only on Windows (actually it's a Windows service), so it's ok to use interfaces IWebBrowser2 and IHtmlDocument2
The function's signature is string function(string). I'm able to run the function in C++, based on this tutorial (I'm using IWebBrowser2 and IHtmlDocument2 interface), but I'm not able to get the output from that JS function back in C++.
Is there any method to retrieve the output from that JS function back in C++, using those interfaces? ( or maybe other)
Thank you,
I'll answer to my own question, if someone will have the same question:
Short answer is you can't obtain the output of javascript script using these interfaces. The IWebBrowser2 and IHtmlDocument are running in a context based on IE, so you can't obtain the output of running scripts.
The solutions for this problem are:
V8 or SpiderMonkey
Active Script Interfaces
If you plan to use V8 in your application, the basic example for calling a function is provided at Calling a v8 javascript function from c++ with an argument (But, be aware of the Dispose() function, which is wrongly placed)
If you plan to use Active Script Interfaces, the basic example is provided at Run JavaScript function from C++ without MFC . It's a useful example that shows how to run a JavaScript function.

Print out webview

How can we print out what an webview contains? i tried something like :
view.page().currentFrame().toPlainText()
but it didn't work
PS: my webview contains an evaluation of a javascript function , so i suppose toHtml() won't do the job any suggestion?
QtWebKit will display javascript-generated webpages exactly the same as it will display any other webpage (though you can disable js through QWebSettings). I assume then, that you want the webpage to generate elements based on requests from the C++ application.
If that's the case, you want QWebFrame::evaluateJavaScript (as previously stated in this previous question: Qt4: How to call JavaScript functions in a page from C++ via QtWebkit?).
More generally, you might find this useful.

Calling javascript from objective-c code

I found a lots of ways to call objective-c code from javascript, but I want to call the javascript code from objective-c. Last time I submitted a HTML FORM from objective-c, and now I wan't to call a javascript method. What do you think, is there any way to call it and get the response? I am interested in any solution, but I started to think and I think I need to send a html call or something like this, but I am not sure about this because the javascript is client side code, so maybe I need to process it from my objective-c code.
What do you think about this?
Are you looking for UIWebView's stringByEvaluatingJavaScriptFromString: method?
This lets you run JS code inside of the sandbox of a web view, from Objective-C.
Sounds like you are using a WebKit embedded in your app to link JavaScript to your "native" Obj-C code right?
If so you simply do this:
NSString* script = #"function __wrapper() { return (typeof webNodeEvent == \"function\") } __wrapper();";
id resultObj = [nodeScriptObject evaluateWebScript:script];
if ([resultObj boolValue]) { // its there, call it
[nodeScriptObject callWebScriptMethod:#"webNodeEvent" withArguments:[NSArray arrayWithObject:eventType]];
}
The nodeScriptObject is from the WebView and is just the JavaScript object.

Categories

Resources