2

It is possible to load an external javascript file from the html using QWebView?

In the following QtProject (all files in the same directory) there is javascript code directly inside the html and also in an external file. I'm missing the external behavior while loading it in QWebView (in the browser it works fine):

MyApp.pro

QT       += core gui webkitwidgets

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = MyApp
TEMPLATE = app
DESTDIR = ./

SOURCES += main.cpp

HEADERS  +=

main.cpp

#include <QApplication>
#include <QtWebKitWidgets>
#include <QFile>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWebView *view = new QWebView;
    view->show();

    QFile file("qt.html");

    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return -1;

    QString html = QTextStream(&file).readAll();
    view->setHtml(html);

    return a.exec();
}

qt.html

<html>

<head>
  <script type="text/javascript" src="qt.js">
  </script>
</head>

<body onload="hello()">

Test..


<script>
    alert("Hello World INTERNAL!");
</script>

</body>
</html> 

qt.js

function hello() {
    alert("Hello World EXTERNAL!");
}
4
  • I've never had a problem with this. Are you sure qt.js is in the same folder as your executable? Or are you using the QRC system? Commented Dec 9, 2015 at 17:40
  • Absolutely sure, not using QRC. Did you tried to build my project? Commented Dec 9, 2015 at 17:43
  • Oh, you know what? You didn't set the base URL -- look at the 2nd param of QWebView::setHtml(). That needs to be set in order for your application to know where to find that Javascript file. Commented Dec 9, 2015 at 18:19
  • Something like view->setHtml(html, QUrl("C:/Users/User/Downloads/MyApp/")); is expected to work? It didn't work here. Commented Dec 9, 2015 at 18:47

3 Answers 3

1

Alternatively 1:

It seems the javascript isn't evaluated from the html. In other words, the following has no effect:

<script type="text/javascript" src="qt.js">
</script>

It must be done explicitly:

QString js = readFile("qt.js");
view->page()->mainFrame()->evaluateJavaScript(js);

And, there is no need to set baseUrl in setHtml().

Alternatively 2:

Use the QRC System and set the baseUrl in setHtml. This way doesn't require a call to view->page()->mainFrame()->evaluateJavaScript();

//    QString js = readFile(":/qt.js");
//    view->page()->mainFrame()->evaluateJavaScript(js);

    QString html = readFile(":/qt.html");
    view->setHtml(html, QUrl("qrc:/"));

application.qrc

<!DOCTYPE RCC><RCC version="1.0">
<qresource>
    <file alias="qt.png">resource/qt.png</file>
    <file alias="image.html">resource/image.html</file>
    <file alias="qt.html">resource/qt.html</file>
    <file alias="qt.js">resource/qt.js</file>
</qresource>
</RCC>
Sign up to request clarification or add additional context in comments.

Comments

0

Actually you can. You just need to add file:// at the begining of your src.

file:///home/js/somejs.js

Comments

0

If you want to load a local html file, first you need to change the src attribute of the javascript, for example:

then src is changed to src="'file:///D:/home/myWeb/js/myjs.js'"

this will work. Relative path can also be given.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.