Please nominate your Qt Champions for 2021!

Scope of variables and Javascript injected with evaluateJavaScript function

  • All,

    My team and I are using Qt 4.8 to build a display application with QtWebKit. We did this previously (last year) and had some ideas about new ways to make some of our development quicker and easier. One such way we want to do this is limit the amount of Javascript code we use to manipulate our display (HTML). We want to take advantage of the evaluateJavaScript function to interact with our webpage without having separate JS files. For example:

    @QString script = "var some_var = 5;";
    script += "alert(var);";

    Now my concern is that some_var is defined globally and I could run into two problems: 1. Collision issues with other methods that create scripts that also have a "some_var" and 2. If I run this function that calls the script that creates "some_var" it is getting created globally over and over again causing memory leak type concerns.

    My real question boils down to this: what is the scope of the javascript that evaluateJavascript() injects into a webpage?

    Any answers would be greatly appreciated,

  • I'm no longer looking for an answer on this one for a few reasons. The first is that my team discovered QWebElement, and we will likely be using that for manipulating our HTML rather than straight JavaScript. The second is that if we do use any JS, we will be using anonymous functions when calling them with evaluateJavascript; they fall out of scope once they are done running. Finally, we feel good design can help us limit the JS we use anyways, so we don't anticipate any issues.


Log in to reply