Results 1 to 3 of 3

Thread: Problem with evaluateJavaScript

  1. #1
    Join Date
    Apr 2009
    Posts
    26
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Problem with evaluateJavaScript

    Hello!

    I have a problem with an injected JavaScript and I don't know why it doesn't work as expected. I use a QWebView and after loading is finished of the current website, I want to inject a JavaScript.

    Here a shorted version of the script:

    Qt Code:
    1. QString injectedScript =
    2.  
    3. "alert('First alert');"
    4.  
    5. "function JSGetElement () {"
    6. "alert('Clicked');"
    7. "}"
    8.  
    9. "function JSOnMouseMove (event) {"
    10. // Making something
    11. "}"
    12.  
    13. "var body = document.body;"
    14. "body.onclick = function () { JSGetElement(); };"
    15. "body.onmousemove = function () { JSOnMouseMove (event); };"
    16. ;
    17.  
    18. QString one =
    19. "var headID = document.head;"
    20. "var newScript = document.createElement('script');"
    21. "newScript.type = 'text/javascript';"
    22. "newScript.innerHTML = " + injectedScript + ";"
    23. "headID.appendChild(newScript);"
    24. ;
    25. ui->webView->page()->mainFrame()->evaluateJavaScript(one);
    To copy to clipboard, switch view to plain text mode 
    The script works if an alert box appears when you click on the website.

    But if you remove the first line ("alert('First alert');") the script doesn't work anymore and no alert box appears when you click on the website. Can anybody say me why this happen or how I must change the script that it works without the first line?

    Thank you.

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

    Default Re: Problem with evaluateJavaScript

    I am no expert on JavaScript but are you sure you don't need any separation between the end of one function declaration and the beginning of the next?

    I.e. is this really valid JavaScript code?
    Qt Code:
    1. function foo() {}function bar() {}var baz = somevalue;
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. #3
    Join Date
    Apr 2009
    Posts
    26
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problem with evaluateJavaScript

    Hi anda_skoa,

    Thank you for your fast reply. I don't know if the style of the JavaScript is good or bad, but it works. After searching over two hours for a solution I found it now. All I need is to wrap the complete script into a self running function:

    Qt Code:
    1. (function(){ ... })();
    To copy to clipboard, switch view to plain text mode 

    I don't know why this is necessary because the script is nested in a <script>...</script>. When I insert the script manually in a local saved website it works without this.

    However now it works

Similar Threads

  1. QWebView and evaluateJavaScript
    By hereiam in forum Qt Programming
    Replies: 3
    Last Post: 13th August 2013, 13:18
  2. Replies: 1
    Last Post: 3rd May 2013, 17:07
  3. webkit evaluatejavascript
    By huyanke in forum Newbie
    Replies: 1
    Last Post: 13th May 2011, 13:27
  4. QWebElement evaluateJavaScript problem
    By Talei in forum Newbie
    Replies: 0
    Last Post: 10th March 2011, 05:38
  5. Problem with evaluateJavaScript()
    By piotr.dobrogost in forum Qt Programming
    Replies: 0
    Last Post: 26th August 2009, 20:36

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.