Reputation: 125
I have qrc file that looks like this:
<qresource prefix="/web">
<file alias="assets.js">../web/assets.js</file>
<file alias="index.html">../web/index.html</file>
</qresource>
Inside assets.js Just adding function for allert popup:
function myFunction()
{
window.alert("Hello from assets.js");
}
Inside index.html adding another javascript for alert popup, loading assets.js and adding 2 buttons. First one to call window popup from external javascript file (assets.js) and second to call the javascript that is embeded into index.html file:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
function localFunction()
{
window.alert('HTML loaded');
}
</script>
<script src="assets.js"></script>
<button onclick="myFunction()">External JS</button>
<button onclick="localFunction()">Local JS</button>
</body>
</html>
Now when I'm trying to load index.html in qtwebkit:
webView->load(QUrl(QStringLiteral("qrc:/web/index.html")));
I can see that index.html has been loaded ok (I can see the 2 buttons ) When clicking on the button that should call the local (html embeded) javascript it works. Clicking the second button does nothing.
It seems like the external assets.js is not loaded properly. Any sugestions how I can get it working?
Thanks.
Upvotes: 2
Views: 1549
Reputation: 4050
Javascript isn't evaluated from the html. Your JS file could not be found. The following has no effect:
<script src="assets.js"></script>
Try it explicitly:
const QString js = readFile("qrc:/web/assets.js"); // Load your javascript file
view->page()->mainFrame()->evaluateJavaScript(js);
Another solution, more efficient: Use the QRC System and set the baseUrl in setHtml to link all files correctly:
const QString html = readFile(":/index.html");
view->setHtml(html, QUrl("qrc:/"));
Upvotes: 1