Qt World Summit: Submit your Presentation

External Javascript in QWebView

  • Hello,

    I am trying to implement screen scraping on some webpages (as the webview kept on throwing panic(bugs have been already reported)). Using Qt 4.6.3 for Symbian.

    I get the form element out of the webview using
    @ QWebView *webView = new QWebView();
    QWebElementCollection formElements = webView->page()->mainFrame()->findAllElements("form");

    QWebElement element;
    for(int i = 0; i < formElements.count(); i++)
        element = formElements.at(i);
        QString attName = element.attribute("name", "-999");
        if(attName.compare("login") == 0)

    After that i load the form element on to a new webiew and evaluate javascript

    @QWebView *ftwWebView = new QWebView();
    QWebFrame *ftwFrame = ftwWebView->page()->mainFrame();
    result = ftwFrame->evaluateJavaScript("document.getElementById('username').value = 'someusername';");
    result = ftwFrame->evaluateJavaScript("document.getElementById('password').value = 'somepassword';");
    result = ftwFrame->evaluateJavaScript("function include(destination) { var e=window.document.createElement('script'); e.setAttribute('src',destination); window.document.body.appendChild(e); } include('http://somelink.com/somefile.js');");
    result = ftwFrame->evaluateJavaScript("validate_form( document.forms[0], true );");

    The validate_form function in located in somefile.js file. If i repeat the same steps in browser it works perfectly but QWebView is failing to run the function.

    My question is will the webview load functions from external .js files if i load the webview using setHtml instead of loading it from an URL.



  • You can evaluate javascript code:

    var fileref = document.createElement("script");
    fileref.setAttribute("type", "text/javascript");
    fileref.setAttribute("src", filename);

  • Hello,

    Thanks for the reply. I am already doing that

    @function include(destination)
    var e=window.document.createElement('script');


    The function is in the js file. In the src attribute the url path is also correct but none of the functions get called. I think it is not auto downloading. Is there a way i can initiate it?



  • I didn't try the code i wrote. But i see that you didn't add type attribute to your var e.

    @fileref.setAttribute(“type”, “text/javascript”);@

    maybe this solves your problem.

  • Hi,

    I tried it with no luck

    @e.setAttribute('type', "text/javascript");@

    I also included this

    @ftwWebView->settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, true);@

    Again with no luck. Can't QWebView load js functions from the link? Or is it because i am setting setHTML?



  • Hello All,

    I found out that evaluate JavaScript won't load/include external(src tag info) links. So i created a new webview, downloaded all the source from the link included it in the webview then i evaluated the JS and it works.



  • Glad to hear that your problem solved. I think this could be a bug. Btw, there have to be a more suitable way to get url content rather than creating webview.

  • Hello,

    This is what i wanted to do:

    1. Get a form element out of web-view.
    2. Load the contents of the form i.e., username and password.
    3. Encrypt the "input" tags wherever necessary using the js functions.
    4. Submit the form.
    5. Get the cookies of that session.

    Initially i tried to open the webview ask the user to enter all the details and then i used to close the webview after taking the cookies. But the webview for some sites used to throw panic on symbian devices. So i took up screen scraping method.

    Now to run the JS i could have used QScriptEngine but there were more than 6-7 interlinked JS files.

    Thanks for the help and this is the best alternate way i found. But i will continue to search as you mentioned this might not be a correct way.



  • hi,
    can u clearly explain how you made js work in webview because i am also facing problem with download link. it is not getting evaluated. can u suggest me how to do it.

  • I load JS files that contain functions into webview frequently using the following method:

    Connect the javaScriptWindowObjectCleared signal from QWebFrame to a local jsCleared function. Note have to use this signal or the js loses context.

    In jsCleared read in the js file that contains your functions. I save my functions in a resource file but you can load in any file from any source (including libraries like jQuery).

    Once the js file has been read in then just use the evaluateJavaScript method to load the entire js file. In my case when I read the resource js text file it is read as a QByteArray and I have to convert it to a QString before using evaluateJavaScript.

    Once the jsCleared method has loaded the js file then you can use any function defined in the file using evaluateJavaScript.

    This is the simplest method I found for loading js libraries (either internal or external) that I don't want to be a permanent part of the HTML file.

    Note: I use this method with Qt 4.8.1 on a desktop.

    Finally I noticed that in your original post you set the result variable to the return value of the evaluateJavaScript method but result is a QVariant object and you have to convert it to the correct data type in order to use it.

Log in to reply