Call JavaScript function with parameters from Qt C++
I know how can I call a JavaScript function without parameters from Qt C++ code:
JavaScript code:
Code:
function foo() {
console.log("bar");
}
Qt code:
Code:
ui->webView->page()->mainFrame()->evaluateJavaScript(foo);
I want to call a JavaScript fuction with parameters from Qt C++ code. How can I do it? It's possible to perform a pass parameters?
Best regards.
Re: Call JavaScript function with parameters from Qt C++
If the parameter value can be expressed as a string, you could simple make it part of the "script"
Code:
const QString scriptWithPlaceHolders
= "foo(%1);";
evaluateJavaScrpt(scriptWithPlaceHolders.arg(argument));
Cheers,
_
Re: Call JavaScript function with parameters from Qt C++
Im having some problems with this.
Ive got this code
Code:
const QString scriptWithPlaceHolders
= "test();null";
((std::pair<QWebView*, std::string>)publicWindows.at(0)).first->page()->mainFrame()->evaluateJavaScript(scriptWithPlaceHolders);
over this javascript
Code:
function test(){
document.getElementById("title").innerHTML = ":(";
}
And everything seems fine, but if i try:
Code:
const QString scriptWithPlaceHolders
= "test(%1);null";
((std::pair<QWebView*, std::string>)publicWindows.at(0)).first->page()->mainFrame()->evaluateJavaScript(scriptWithPlaceHolders.arg(":("));
over
Code:
function test(purq){
document.getElementById("title").innerHTML = ":(";
}
it does not work, any idea on what can be wrong?
Re: Call JavaScript function with parameters from Qt C++
Please explain how "does not work" manifest itself.
Re: Call JavaScript function with parameters from Qt C++
If I were a betting man, I would bet you need to include some escaped quotations so that your parameter gets passed as a string to your function.
Re: Call JavaScript function with parameters from Qt C++
does not work was function not being called, but sulliwk was right it worked doing this:
evaluateJavaScript(scriptWithPlaceHolders.arg("':( '"));
Some wasted hours blaming everything else xD. But i really cant understand why is that even needed, more when thinking on javascript and his weak typing.
Thank you dudes.