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.