1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the documentation of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:FDL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
17 ** GNU Free Documentation License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Free
19 ** Documentation License version 1.3 as published by the Free Software
20 ** Foundation and appearing in the file included in the packaging of
21 ** this file. Please review the following information to ensure
22 ** the GNU Free Documentation License version 1.3 requirements
23 ** will be met: http://www.gnu.org/copyleft/fdl.html.
26 ****************************************************************************/
29 \page qdeclarativejavascript.html
30 \title Integrating JavaScript
32 QML encourages building UIs declaratively, using \l {Property Binding} and the
33 composition of existing \l {QML Elements}. To allow the implementation of more
34 advanced behavior, QML integrates tightly with imperative JavaScript code.
36 The JavaScript environment provided by QML is stricter than that in a web browser.
37 In QML you cannot add, or modify, members of the JavaScript global object. It
38 is possible to do this accidentally by using a variable without declaring it. In
39 QML this will throw an exception, so all local variables should be explicitly
42 In addition to the standard JavaScript properties, the \l {QML Global Object}
43 includes a number of helper methods that simplify building UIs and interacting
44 with the QML environment.
46 \section1 Inline JavaScript
48 Small JavaScript functions can be written inline with other QML declarations.
49 These inline functions are added as methods to the QML element that contains
54 function factorial(a) {
59 return a * factorial(a - 1);
64 onClicked: console.log(factorial(10))
69 As methods, inline functions on the root element in a QML component can be
70 invoked by callers outside the component. If this is not desired, the method
71 can be added to a non-root element or, preferably, written in an external
74 \section1 Separate JavaScript files
76 Large blocks of JavaScript should be written in separate files. These files
77 can be imported into QML files using an \c import statement, in the same way
78 that \l {Modules}{modules} are imported.
80 For example, the \c {factorial()} method in the above example for \l {Inline JavaScript}
81 could be moved into an external file named \c factorial.js, and accessed like this:
84 import "factorial.js" as MathFunctions
88 onClicked: console.log(MathFunctions.factorial(10))
93 Both relative and absolute JavaScript URLs can be imported. In the case of a
94 relative URL, the location is resolved relative to the location of the
95 \l {QML Document} that contains the import. If the script file is not accessible,
96 an error will occur. If the JavaScript needs to be fetched from a network
97 resource, the component's \l {QDeclarativeComponent::status()}{status} is set to
98 "Loading" until the script has been downloaded.
100 Imported JavaScript files are always qualified using the "as" keyword. The
101 qualifier for JavaScript files must be unique, so there is always a one-to-one
102 mapping between qualifiers and JavaScript files. (This also means qualifiers cannot
103 be named the same as built-in JavaScript objects such as \c Date and \c Math).
106 \section2 Code-Behind Implementation Files
108 Most JavaScript files imported into a QML file are stateful, logic implementations
109 for the QML file importing them. In these cases, for QML component instances to
110 behave correctly each instance requires a separate copy of the JavaScript objects
113 The default behavior when importing JavaScript files is to provide a unique, isolated
114 copy for each QML component instance. The code runs in the same scope as the QML
115 component instance and consequently can can access and manipulate the objects and
118 \section2 Stateless JavaScript libraries
120 Some JavaScript files act more like libraries - they provide a set of stateless
121 helper functions that take input and compute output, but never manipulate QML
122 component instances directly.
124 As it would be wasteful for each QML component instance to have a unique copy of
125 these libraries, the JavaScript programmer can indicate a particular file is a
126 stateless library through the use of a pragma, as shown in the following example.
132 function factorial(a) {
137 return a * factorial(a - 1);
141 The pragma declaration must appear before any JavaScript code excluding comments.
143 As they are shared, stateless library files cannot access QML component instance
144 objects or properties directly, although QML values can be passed as function
148 \section2 Importing One JavaScript File From Another
150 If a JavaScript file needs to use functions defined inside another JavaScript file,
151 the other file can be imported using the \l {QML:Qt::include()}{Qt.include()}
152 function. This imports all functions from the other file into the current file's
155 For example, the QML code below left calls \c showCalculations() in \c script.js,
156 which in turn can call \c factorial() in \c factorial.js, as it has included
157 \c factorial.js using \l {QML:Qt::include()}{Qt.include()}.
161 \o {1,2} \snippet doc/src/snippets/declarative/integrating-javascript/includejs/app.qml 0
162 \o \snippet doc/src/snippets/declarative/integrating-javascript/includejs/script.js 0
164 \o \snippet doc/src/snippets/declarative/integrating-javascript/includejs/factorial.js 0
167 Notice that calling \l {QML:Qt::include()}{Qt.include()} imports all functions from
168 \c factorial.js into the \c MyScript namespace, which means the QML component can also
169 access \c factorial() directly as \c MyScript.factorial().
172 \section1 Running JavaScript at Startup
174 It is occasionally necessary to run some imperative code at application (or
175 component instance) startup. While it is tempting to just include the startup
176 script as \e {global code} in an external script file, this can have severe limitations
177 as the QML environment may not have been fully established. For example, some objects
178 might not have been created or some \l {Property Binding}s may not have been run.
179 \l {QML JavaScript Restrictions} covers the exact limitations of global script code.
181 The QML \l Component element provides an \e attached \c onCompleted property that
182 can be used to trigger the execution of script code at startup after the
183 QML environment has been completely established. For example:
187 function startupFunction() {
191 Component.onCompleted: startupFunction();
195 Any element in a QML file - including nested elements and nested QML component
196 instances - can use this attached property. If there is more than one \c onCompleted()
197 handler to execute at startup, they are run sequentially in an undefined order.
199 Likewise, the \l {Component::onDestruction} attached property is triggered on
200 component destruction.
203 \section1 JavaScript and Property Binding
205 Property bindings can be created in JavaScript by assigning the property with a \c function
206 that returns the required value.
208 See \l {qml-javascript-assignment}{Property Assignment versus Property Binding} for details.
211 \section1 Receiving QML Signals in JavaScript
213 To receive a QML signal, use the signal's \c connect() method to connect it to a JavaScript
216 For example, the following code connects the MouseArea \c clicked signal to the \c jsFunction()
221 \o \snippet doc/src/snippets/declarative/integrating-javascript/connectjs.qml 0
222 \o \snippet doc/src/snippets/declarative/integrating-javascript/script.js 0
225 The \c jsFunction() will now be called whenever MouseArea's \c clicked signal is emitted.
227 See \l{QML Signal and Handler Event System#Connecting Signals to Methods and Signals}
228 {Connecting Signals to Methods and Signals} for more information.
231 \section1 QML JavaScript Restrictions
233 QML executes standard JavaScript code, with the following restrictions:
236 \o JavaScript code cannot modify the global object.
238 In QML, the global object is constant - existing properties cannot be modified or
239 deleted, and no new properties may be created.
241 Most JavaScript programs do not intentionally modify the global object. However,
242 JavaScript's automatic creation of undeclared variables is an implicit modification
243 of the global object, and is prohibited in QML.
245 Assuming that the \c a variable does not exist in the scope chain, the following code
249 // Illegal modification of undeclared variable
251 for (var ii = 1; ii < 10; ++ii)
253 console.log("Result: " + a);
256 It can be trivially modified to this legal code.
260 for (var ii = 1; ii < 10; ++ii)
262 console.log("Result: " + a);
265 Any attempt to modify the global object - either implicitly or explicitly - will
266 cause an exception. If uncaught, this will result in an warning being printed,
267 that includes the file and line number of the offending code.
269 \o Global code is run in a reduced scope
271 During startup, if a QML file includes an external JavaScript file with "global"
272 code, it is executed in a scope that contains only the external file itself and
273 the global object. That is, it will not have access to the QML objects and
274 properties it \l {QML Scope}{normally would}.
276 Global code that only accesses script local variable is permitted. This is an
277 example of valid global code.
280 var colors = [ "red", "blue", "green", "orange", "purple" ];
283 Global code that accesses QML objects will not run correctly.
286 // Invalid global code - the "rootObject" variable is undefined
287 var initialPosition = { rootObject.x, rootObject.y }
290 This restriction exists as the QML environment is not yet fully established.
291 To run code after the environment setup has completed, refer to
292 \l {Running JavaScript at Startup}.
294 \o The value of \c this is currently undefined in QML in the majority of contexts
296 The \c this keyword is supported when binding properties from JavaScript.
297 In all other situations, the value of
298 \c this is undefined in QML.
300 To refer to any element, provide an \c id. For example:
304 width: 200; height: 100
305 function mouseAreaClicked(area) {
306 console.log("Clicked in area at: " + area.x + ", " + area.y);
308 // This will not work because this is undefined
310 height: 50; width: 200
311 onClicked: mouseAreaClicked(this)
313 // This will pass area2 to the function
316 y: 50; height: 50; width: 200
317 onClicked: mouseAreaClicked(area2)