Results 1 to 10 of 10

Thread: Call a JavaScript function from C++ using QtWebkit

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2014
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Call a JavaScript function from C++ using QtWebkit

    Quote Originally Posted by Spitfire View Post
    As an alternative you can try QWebPage::currentFrame() or QWebPage::frameAt().

    If you don't know how to call js inside iFrame from parent document consider this:
    Qt Code:
    1. QWebView::page()->mainFrame()->evaluateJavaScript( "function call_iFrame() { document.getElementById('iframe_id').contentWindow.method_inside_iframe(); }" )
    To copy to clipboard, switch view to plain text mode 
    That should get you going.
    I am sorry I am resurrecting this topic, but following it I think this topic is incomplete and my question might start from this point.
    Into an application that use a WebView I am loading some web content. Trying to execute a JavaScript code that call a alert() window directly into evaluateJavaScript() is fine, but calling it into a function it fails.
    Qt Code:
    1. QString strTest1 ("alert(\"this is sly!\"); null ");
    2. QVariant f1result = webView->page()->mainFrame()->evaluateJavaScript( strTest1 ); // fine
    3. QString strTest2 ("function alert_something() { alert(\"this is sly!\"); } ");
    4. QVariant f2result = webView->page()->mainFrame()->evaluateJavaScript( strTest2 ); // bad
    5.  
    6. qDebug() << f2result.toString(); // returns nothing
    To copy to clipboard, switch view to plain text mode 
    I can't figure why calling that trivial function call fails... Ideas?
    Last edited by SlyMaximus; 13th May 2014 at 21:54.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Call a JavaScript function from C++ using QtWebkit

    Your second JavaScript snippet only contains a function definition, not a call to that function.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    SlyMaximus (14th May 2014)

  4. #3
    Join Date
    May 2014
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Call a JavaScript function from C++ using QtWebkit

    Quote Originally Posted by anda_skoa View Post
    Your second JavaScript snippet only contains a function definition, not a call to that function.

    Cheers,
    _
    Loolll! I think I was blind, of-course we need that call.
    10x!

Similar Threads

  1. QTWebKit, out of control Javascript CPU usage
    By Dkamerad in forum Qt Programming
    Replies: 2
    Last Post: 12th December 2014, 05:01
  2. [QtWebKit] Inject JavaScript properly when DOM loads..
    By fab_74 in forum Qt Programming
    Replies: 2
    Last Post: 27th March 2011, 17:58
  3. Replies: 2
    Last Post: 15th November 2010, 05:17
  4. QtWebkit and JavaScript error messages
    By Diph in forum Qt Programming
    Replies: 1
    Last Post: 29th April 2009, 11:10
  5. add custom HTML attribute by javascript in QtWebKit
    By sand.fj.wen in forum Qt Programming
    Replies: 3
    Last Post: 17th April 2009, 07:25

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.