0

While I was developing an application on Code Name One on the simulator I faced strange problem.

I have a Tabs with a single Tab , this tab has a browserComponent with HTML content. It has span element which call a JavaScript function onclick event, this JavaScript function call a Java method which create a new Tab and add it to the Tabs.

Everything at this moment goes well, but the BrowserComponent starts loading the CodeNameONe home page by itself without any instruction from me, so I was wondering if it's a bug or what. I'm using NetBeans 8.2 with the last version of CodeNameOne.

NOTE I called a Java method from JS function in another example with on Tabs and the browser didn't load any external pages so I think is related to Tabs more than any thing else.

NOTE I am sure no problem related to the code but I posted it after being advised to do so.

This is the JavaScript function:

 function openMessageFaceJS()
  {
        window.openMessageFace();                 
  }

and this is the Java method

this.context.getWindow().//context is a JSObject
            set("openMessageFace", (JSFunction) (who, are_you) ->
            {
                manager.openMessageFace();
                //look blow to see this method openMessageFace();
            });

And in another class the actual method code

 public void openMessageFace()
 {
    //this operation is performed successfuly
    Component component = create1();

    tabs.addTab("what (:", component);
    sendMessageFacePosotiin = tabs.getTabCount() - 1;
    tabs.setSelectedIndex(sendMessageFacePosotiin, true);
 }
halfer
  • 19,824
  • 17
  • 99
  • 186
Anas
  • 688
  • 7
  • 24
  • And how exactly do you expect anyone to help if we can't see the actual code? – Jim Garrison Aug 13 '17 at 19:49
  • @JimGarrison thanks for interesting i didn't upload the code due to two reasons **1)** the code is too long distributed between many classes **2)** when I call `Java` method from `JS` function it worked fine but not in the `Tabs` so I don't think it has a relation with the code – Anas Aug 13 '17 at 19:52
  • Can you stop overusing code formatting please? It's for actual code, not the names of programming languages. – bcsb1001 Aug 13 '17 at 19:56
  • @bcsb1001 okay thanks brother, i'll follow you advice :) – Anas Aug 13 '17 at 19:59
  • Can you clarify how this is different and with which device type? Is this only on the simulator and works on Android or the other way around? – Shai Almog Aug 14 '17 at 05:20
  • BrowserComponent acts differently in the simulator (in which it uses a lightweight and limited cross platform browser) than a real device (in which it uses the native browser), so please consider this difference and do a test on a real device. Moreover try to search in your code all the uses of the "setUrl" method. – Francesco Galgani Aug 14 '17 at 10:03
  • Thanks @ShaiAlmog for interesting yes it's on the **simulator** and worked fine on **Android** In other words the browser on the simulator opens a web page by itself – Anas Aug 14 '17 at 13:42
  • Thanks @AlwaysSmiling for interesting yes you are completely right but the simulator itself uses **JavaFX** `WebView` and it's has a very powerful `JavaScript Engine` have a [look here](https://stackoverflow.com/questions/30104124/what-javascript-engine-used-inside-javafx) I think it's a bug in the **simulator** – Anas Aug 14 '17 at 13:49

1 Answers1

1

You are calling the callback before the page is finished loading so the "bridge" hasn't been completely set up yet. The safest thing to do is place all of your JS bridge stuff inside the onLoad web event to guarantee that setup has been completed.


BrowserComponent bc = new BrowserComponent();
bc.addWebEventListener("onLoad", e->{
    JavascriptContext ctx = new JavascriptContext(bc);
    ... etc....
});

That said, I have made a small fix that should guard against this problem here

It will be available in the next library update (Friday I believe)

steve hannah
  • 4,586
  • 11
  • 18
  • Thanks @steve for interesting but the callback "Java" method is called successfully so the page is completely loaded successfully and also when is used the code you posted the problem still, **the problem** is literally "After the callback Java method is called successfully the browser loads a page by itself this page is codename one home page – Anas Aug 14 '17 at 19:09
  • I also suggest creating a waiting queue for scripts that is tried to be executed before page completely loaded and when it loaded successfully we execute them sequentially -(: – Anas Aug 14 '17 at 19:13
  • Steve, is it possible that this is a side-effect of this commit? https://github.com/codenameone/CodenameOne/commit/ce5bbd28e5e1bc297f292ccf30e550de08810250 – Shai Almog Aug 15 '17 at 03:56
  • @ShaiAlmog Yes. That is the commit. It was necessary for Android. However, that commit didn't "break" it. It just made the existing break more visible. I have fixed the issue here https://github.com/codenameone/CodenameOne/commit/15a9d49ea73148a73ff5bd5a983b56483c5e9e89 – steve hannah Aug 15 '17 at 14:03
  • @Anas "but the callback "Java" method is called successfully so the page is completely loaded successfully " You can't conclude that. There are some pieces that are put in place on page load. If you start working with the javascript context before page load, you're introducing race conditions that may or may not bite you. – steve hannah Aug 15 '17 at 14:12
  • @stevehannah but I updated the code as you mentioned `bc.addWebEventListener("onLoad", e->{ JavascriptContext ctx = new JavascriptContext(bc); //calls.. });` but I still have the same problem so what should I do ? – Anas Aug 15 '17 at 22:50
  • I would need to see your code (or at least the relevant parts) to comment further. – steve hannah Aug 16 '17 at 05:06