EvaluateJavaScript



  • Hi, everybody! I just installed here Qt 4.7 SDK, but I'm having a problem with WebView. I want to test the method evaluateJavaScript, but I just can't get it to work. Here is my code:

    @
    import Qt 4.7
    import QtWebKit 1.0

    Rectangle {
    id: rect
    width: 700
    height: 500

    WebView {
        id: webview
        anchors.fill: parent
        url: "http://www.google.com"
    
        onLoadFinished: {
            console.log("loaded.");
            evaluateJavaScript("alerta('test!')")
        }
    }
    

    }
    @

    Any tip on what I'm doing wrong?

    ps: I'm running it on Qt Creator on Windows 7 64bits
    ps2: I know the js call "alerta" is wrong, but this forum won't let me save this post with the correct name, saying that I'm a spammer if I do that



  • Update: I made evaluateJavaScript work, but only when calling predefined js methods:

    @webview.evaluateJavaScript("window.qml.qmlCall()")@

    So, any tips on why can't I call default html/js methods like alert or document.getElementById?



  • Hi,

    In the alert case, the onAlert signal handler of the WebView should be called. If you add
    @onAlert: console.log(message)@
    To the WebView in the code above, are you able to get the correct output?

    Regards,
    Michael



  • hi. I think the problem is this:

    @evaluateJavaScript("alerta('test!')")@

    if should be alter not alerta

    @evaluateJavaScript("alert('test!')")@



  • Hi 2beers. I know it's wrong, but when I tried to create this forum post with the "alert" word, the system said it was a SPAM, and did not let me save it. That's why I wrote "alerta"



  • [quote author="mbrasser" date="1285648352"]Hi,

    In the alert case, the onAlert signal handler of the WebView should be called. If you add
    @onAlert: console.log(message)@
    To the WebView in the code above, are you able to get the correct output?

    Regards,
    Michael[/quote]

    That worked! But I still can't use other methods, like getElementById, like this:
    @evaluateJavaScript("document.getElementById('body') . innerHTML = 'aaaa'")@

    Just to be sure, I ran this on Firebug, and it works on www.google.com:
    @document.getElementById('body') . innerHTML = 'aaaa'@

    But it doesn't work with evaluateScript



  • I'm not sure what's happening. You could try the same test in QWebFrame (from C++) to see if it works as expected there (WebView just forwards the evaluation to it's internal QWebFrame)



  • Did you ever get evaluateJavaScript working with document.getElementById?

    I have:

    @console.log("document = " + webView.evaluateJavaScript("document"))
    console.log("field = " + webView.evaluateJavaScript("document.getElementById('txtDefaultLastName')"))
    console.log("value = " + webView.evaluateJavaScript("document.getElementById('txtDefaultLastName').value"))@

    ...and get:

    @document = [object Object]
    field = undefined
    value = undefined@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.