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

Native Development

Reply
Highlighted
New Contributor
Posts: 4
Registered: ‎01-20-2011
My Device: Q10
My Carrier: AT&T

Working with WebView and evaluateJavaScript/javaScriptResult

[ Edited ]

I am trying to understand how to retrieve the results from javaScriptResult with in a WebView.  

 

There are a few discussions on the board but no true examples.

 

I've tried the following code:

  • The first evaluateJavaScript gives a alert result.
  • The second evaluateJavaScript gives a token but I can't get the actual result.

 

import bb.cascades 1.0
import bb.system 1.0

Page {
    attachedObjects: [
        SystemToast {
            id: toast
        }
    ]
    property int token
    property variant result
    Container {
        ScrollView {
        	WebView {
            	url: "http://demo.mdemo.me/example_giveaway"
            	onLoadingChanged: {
                	if (loadRequest.status == WebLoadStatus.Succeeded) {

                        evaluateJavaScript("alert('Result: ' + document.getElementsByTagName('input').length);")
                        
                        token = evaluateJavaScript("return document.getElementsByTagName('input').length;")
                        javaScriptResult(token, result)
                        
                        toast.body = token + " " + result
                        toast.show()
                    }
                }
            }
        }
    }
}

 Thanks...

Developer
Posts: 1,177
Registered: ‎03-20-2013
My Device: Red LE Developer Z10
My Carrier: Fido

Re: Working with WebView and evaluateJavaScript/javaScriptResult

Hello, and welcome to the forums.

 

Token is a QVariant and the type of value it holds is dependent on the nature of the JavaScript code you execute. In your case your JavaScript returns an array of elements from the page DOM, which cannot be printed directly just by concatenating it to a string. Also, I notice that you are calling javaScriptResult as a function, when it is actually a signal generated by the WebView. You cannot call javaScriptResult  yourself. When the evaluateJavaScript() function finishes, the WebView will emit the javaScriptResult signal, where you can process the the token and/or result as needed.

 

It appears you may not understand what a signal is and how it works. I strongly suggest you read and understand this before trying to proceed with your coding.



Developer of Built for BlackBerry certified multiFEED RSS/Atom feed reader and aggregator.  multiFEED Icon

Play nice: Clicking Like Button on posts that helped you not only encourages others to continue sharing their experience, but also improves your own rating on this board. Also, don't forget to accept a post if it solves your problem or answers your question.
New Contributor
Posts: 4
Registered: ‎01-20-2011
My Device: Q10
My Carrier: AT&T

Re: Working with WebView and evaluateJavaScript/javaScriptResult

Thanks for the course correction...  If I had actually absorbed what I was reading in the refrence I would have noticed it was a signal...

 

import bb.cascades 1.0
import bb.system 1.0

Page {
    attachedObjects: [
        SystemToast {
            id: toast
        }
    ]
    property int token
    Container {
        ScrollView {
        	WebView {
            	url: "http://demo.mdemo.me/example_giveaway"
            	onLoadingChanged: {
                	if (loadRequest.status == WebLoadStatus.Succeeded) {
                        token = evaluateJavaScript("return document.getElementsByTagName('input').length;") 
                    }
                }
                onJavaScriptResult: {
                    if(resultId == token)
                    {
                    	toast.body = resultId.toString()
                    	toast.show()
                    }
                }
            }
        }
    }
}

 

Still trying to figure out to work with the const QVariant &result, more reading to do...

 

 

Developer
Posts: 1,177
Registered: ‎03-20-2013
My Device: Red LE Developer Z10
My Carrier: Fido

Re: Working with WebView and evaluateJavaScript/javaScriptResult

I am getting ready to go out of town for two days so I don't have time to test this myself, but while getElementById() is unique and returns a single element, getElementsByTagName() returns an array of elements, even if there is just one matching element, since TagName is NOT unique.

 

First off you need to specify which index to use in the array. If there could be more than one you can loop through them with a for loop.

 

Secondly, what both those functions return are DOM elements, not simple strings that can be printed. If you want to print then you have to decide what you want to print... OuterText, InnerText, an attribute, etc. I think you need to read up on DOM as well as QML.

 



Developer of Built for BlackBerry certified multiFEED RSS/Atom feed reader and aggregator.  multiFEED Icon

Play nice: Clicking Like Button on posts that helped you not only encourages others to continue sharing their experience, but also improves your own rating on this board. Also, don't forget to accept a post if it solves your problem or answers your question.