Nashorn webview issue

Sundararajan Athijegannathan sundararajan.athijegannathan at oracle.com
Wed Dec 9 12:52:32 UTC 2015


Are you using any JSAdapter over netscape.javascript.JSObject instances? 
If so, that is no longer needed! Nashorn natively supports Browser 
JSObjects - you can access properties/functions from JSObjects directly. 
Also you can pass nashorn functions (or assign callbacks). See for 
example: 
http://hg.openjdk.java.net/jdk9/dev/nashorn/file/c779bd47d648/samples/browser_dom.js

-Sundar

On 12/9/2015 6:10 PM, Jim Laskey (Oracle) wrote:
> Which version of jjs are you using? (jjs -version)
>
> — Jim
>
>
>> On Dec 1, 2015, at 1:11 AM, Chris Root <hammer65 at gmail.com> wrote:
>>
>> I'm trying to get two way communication working for a browser project in
>> Nashorn and I'm having trouble implementing an HTML to host callback. I
>> tried this in Java using setMember and it works perfectly, but I can't seem
>> to duplicate it with Nashorn. Since using setMember didn't work I decided
>> to use the approach below but still no luck.
>>
>> I don't want to pick up objects from the window object, I want to plant a
>> function in window that will call Nashorn code. Again getting this to work
>> in Java was no problem. I followed the instructions on this page
>>
>> https://blogs.oracle.com/nashorn/entry/porting_from_the_browser_to
>>
>> I have the jsObject wrapper shown in the article and it works fine. but the
>> code below doesn't work. This is the relevant snip from the load handler.
>>
>> This.engine.loadWorker.stateProperty().addListener(new ChangeListener() {
>>     changed: function(value, oldState, newState) {
>>       switch(newState){
>>         case Worker.State.SUCCEEDED:
>>           This.document = wrap(This.engine.executeScript("document"));
>>           This.window = wrap(This.engine.executeScript("window"));
>>           This.window.hello = function(){
>>             This.hello();
>>           }
>>
>> As you can see I planted a function on the window called hello, which calls
>> a method of the webview wrapper (same wrapper as in the article) called
>> hello()
>>
>> This.hello = function(){
>>   print("****** hello it worked *******");
>> }
>>
>> I then set up the alert handler
>>
>> This.engine.onAlert = new javafx.event.EventHandler() {
>>   handle: function(evt) {
>>     print(evt.data)
>>   }
>> };
>>
>> and then finally loaded the HTML file below into the webview.
>>
>> <html>
>> <head>
>>   <title>Nashorn Browser</title>
>>
>>   <script type="text/Javascript">
>>     function sayhello(){
>>       try{
>>         hello();
>>       }catch(e){
>>         alert(e.message)
>>       }
>>     }
>>   </script>
>>
>> </head>
>> <body>
>>   <a href="#" onclick="sayhello(); return false;">Say Hello</a>
>> </body>
>> </html>
>>
>> What I get back in the console is.
>>
>> JavaRuntimeObject is not a function (evaluating 'window.hello()')
>>
>> Is there any way to establish a callback into my Nashorn code the way it
>> can be done in Java?



More information about the nashorn-dev mailing list