Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    EvaluateJavaScript

    QML and Qt Quick
    4
    8
    10156
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      puelocesar last edited by

      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

      1 Reply Last reply Reply Quote 0
      • P
        puelocesar last edited by

        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?

        1 Reply Last reply Reply Quote 0
        • M
          mbrasser last edited by

          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

          1 Reply Last reply Reply Quote 0
          • 2
            2beers last edited by

            hi. I think the problem is this:

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

            if should be alter not alerta

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

            1 Reply Last reply Reply Quote 0
            • P
              puelocesar last edited by

              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"

              1 Reply Last reply Reply Quote 0
              • P
                puelocesar last edited by

                [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

                1 Reply Last reply Reply Quote 0
                • M
                  mbrasser last edited by

                  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)

                  1 Reply Last reply Reply Quote 0
                  • A
                    aflegg last edited by

                    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@

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post