Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. EvaluateJavaScript
Qt 6.11 is out! See what's new in the release blog

EvaluateJavaScript

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 4 Posters 11.3k Views 1 Watching
  • 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 Offline
    P Offline
    puelocesar
    wrote on last edited by
    #1

    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
    0
    • P Offline
      P Offline
      puelocesar
      wrote on last edited by
      #2

      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
      0
      • M Offline
        M Offline
        mbrasser
        wrote on last edited by
        #3

        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
        0
        • 2 Offline
          2 Offline
          2beers
          wrote on last edited by
          #4

          hi. I think the problem is this:

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

          if should be alter not alerta

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

          1 Reply Last reply
          0
          • P Offline
            P Offline
            puelocesar
            wrote on last edited by
            #5

            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
            0
            • P Offline
              P Offline
              puelocesar
              wrote on last edited by
              #6

              [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
              0
              • M Offline
                M Offline
                mbrasser
                wrote on last edited by
                #7

                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
                0
                • A Offline
                  A Offline
                  aflegg
                  wrote on last edited by
                  #8

                  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
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved