Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

how to call a java fuction from html page and getting the result back in java class

I am facing a typical problem.I want to call a Java fuction from html page and getting the result back in java class.So when ia m using raw HTML data

String htmlContent="<html>" +
        "<script language='JavaScript'> function myfun() { alert('my fun will invoke java function'); " +
        " interesting.test(); return false; }  function message2(m){alert(m);}</script>" +            // calling java function        
        "<body>this is test html<b>bold text </b>" +
        " <a href='' onclick='return myfun()'>link</a>" +
        " <div id='label1'></div>" +
        "</body></html>";

 The below block of code executs fine

mBrowserField.extendScriptEngine("interesting.test", 
                new ScriptableFunction() { 
                    public Object invoke(Object thiz, Object[] args) 
                    { 
                        UiApplication.getUiApplication().invokeLater(new Runnable() {
                            public void run() {
                                Dialog.alert("Java function call from javascript");
                            }
                    });
                    return "";
                }
            });

 Whereas the same raw HTML data if i make an html page like index.html and access it using

    mBrowserField.requestContent(url);

 instead of 

mBrowserField.displayContent(url,"http://localhost");

 It is not at all executing "

extendScriptEngine

method when i click the link.Please help

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: how to call a java fuction from html page and getting the result back in java class

Silly question perhaps.  Does JavaScript actually work on the page in general?

 

Can you clarify in your question when you are talking about JavaScript and Java. 

Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: how to call a java fuction from html page and getting the result back in java class

I want an intercommunication between my htmp page and java code,through javascrip functions. The below method is intended to handle javascript related things .And yes javascript does work on pages in general

mBrowserField.extendScriptEngine
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: how to call a java fuction from html page and getting the result back in java class

[ Edited ]

 

I don't know what you are doing wrong, but following you wil find some code that works fine for me.

 

Here is the code that works:

 

 

public final class MyScreen extends MainScreen {
    public MyScreen() {
    
        setTitle("Javascript Integration Test");

        InputStream content = getClass().getResourceAsStream("/test.html");     
        try {
           byte[] html = IOUtilities.streamToBytes(content);
           BrowserFieldConfig config = new BrowserFieldConfig();
           config.setProperty(BrowserFieldConfig.JAVASCRIPT_ENABLED,Boolean.TRUE);
           BrowserField contentField = new BrowserField(config);
           try {
	           contentField.extendScriptEngine("interesting.test", 
	                   new ScriptableFunction() { 
	                       public Object invoke(Object thiz, Object[] args) { 
	                           UiApplication.getUiApplication().invokeLater(new Runnable() {
	                               public void run() {
	                                   Dialog.alert("Java function call from javascript");
	                               }
	                       });
	                       return "";
	                   }
	               });
           } catch (Throwable t) {
        	   t.printStackTrace();
           }
           this.add(contentField);

           contentField.requestContent("http://<my URL>/test.html");
        } catch (IOException e) {
           e.printStackTrace();
        }

    
    }
}

 

 and here is the file I was referencing from a web site:

 

<html>
    <script language='JavaScript'>
    function myfun() {
    	alert('my fun will invoke java function');
        interesting.test();
        return false;
    } 
    </script>
    <body id="welcome-page">
        <h1>Test page</h1>
        <a href='' onclick='return myfun()'>test</a>
        <div id='label1'><p>Click to test...</p></div>
    </body>
</html>

 

One final thing, you said....

 

"I want an intercommunication between my htmp page and java code,through javascrip functions"

 

you might want to re-read your posts to make sure that they do make sense and are correct.  In our industry, you have to be accurate don't you?  I knew what you meant this time, but might get it wrong next time....

Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: how to call a java fuction from html page and getting the result back in java class

@peter One strange thing i am facing,when i load the page in browserfield,and click on the link,the javascript engine is only invoked from the 2nd time.When i click for the first time nothing happens,and if i reclick again,my javascript functionality is happening.Is it due to some issue in rendering the page?

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: how to call a java fuction from html page and getting the result back in java class

Using the code above, the click worked the first time, every time.

 

I suggest you test the code above in your own environment, does the click problem occur with it? 

Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: how to call a java fuction from html page and getting the result back in java class

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0,maximum-scale=1.0,user-scalable=no,height=device-height"  />
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script language="javascript" type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.blockUI.js"></script>
<script language="javascript" type="text/javascript" src="js/widget.js"></script>
<script type="text/jscript">
function getcontact2(){
	interesting.test();
	}
function onSelectedContact(message) {
              var smallImage = document.getElementById('mobileNo');
              smallImage.value = message;
        }
</script>
</head>
<body>
<div id="wrapper"><div><div id="R1C1"><div id="SECTR1C1S1"><div class="titlebg">
    <div class="head-prevarr"><a href="myairtimeacc.htm"><img src="images/prev-arrow.png" /></a></div>
    Transfer Balance</div>
  <div class="maincontent">
    <div class="banner-cont1"></div>
    <div class="maincontent1 whitecont marg-top">
      <label>Enter your mobile number</label>
      <input type="text" id="mobileNo" placeholder="Mobile number" class="txtfield" />
      &nbsp;&nbsp;&nbsp;
      <input type="button" value=" " class="addbook-btn" href='' onclick="getcontact2();" />
      <label>Enter your Amount</label>
      <input type="text"  placeholder="Enter Amount" />
      <br />
      <div class="btn-cont">
        <a href='' onclick='return getcontact2()'>Cick Here</a>
        <input type="button" class="bnt-yell" value="Proceed" href='' onclick="getcontact2();" />
      </div>
    </div>
  </div></div></div></div></div>
</body>
</html>

 @Peter This is my HTML page.There are two places from where i am calling the "gecontact()2" method.One is 

 <input type="button" value=" " class="addbook-btn" href='' onclick="getcontact2();" />

 and the second is 

 <a href='' onclick='return getcontact2()'>Cick Here</a>

 Now what is happening,when the page loads and if i click on the first calling part,it doesnt work,wheras is if i first click on the href "Click Here" it works and from than onwards if i click the first one it works.But untill and unless i click the href first,the first calling part doesnt work.Anything wrong with my html?

Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: how to call a java fuction from html page and getting the result back in java class

<!DOCTYPE html>

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0,maximum-scale=1.0,user-scalable=no,height=device-height"  />
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script language="javascript" type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script language="javascript" type="text/javascript">
function getcontact2(){
	interesting.test();
	}
</script>
</head>
<body>
 <a href='' onclick='return getcontact2()'>link</a>
      <div class="btn-cont">
        <input type="button"  value="Proceed" href='' onclick="return getcontact2()" />
      </div>
    
</body>
</html>

 Even with this simple html i have figured out that once the "link" hyperlink is not clikced,the "Proceed" button's click doesnt work,why is it?

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: how to call a java fuction from html page and getting the result back in java class

Have you tested my code as I asked?

Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: how to call a java fuction from html page and getting the result back in java class

What is the significance of the byte []

 InputStream content = getClass().getResourceAsStream("/test.html");     
        try {
           byte[] html = IOUtilities.streamToBytes(content);

 and also <myURL>,i am actually reading displaying the content from SDCard