[Qt-interest] Reference Error: google is not defined

Jason H scorp1us at yahoo.com
Fri Mar 4 19:52:42 CET 2011


Kent posted a nice overview of what you need to do.

http://labs.qt.nokia.com/2011/03/04/faking-a-web-browser-environment-in-qtscript/


Could we collect these fakeries in some common place? That way QtScipt/QML can 
benefit from existing JS libraries.




----- Original Message ----
From: Rohan Prabhu <rohan at rohanprabhu.com>
To: Karthick <sppkarthick at gmail.com>
Cc: qt-interest at qt.nokia.com; qt-interest at trolltech.com
Sent: Fri, March 4, 2011 10:59:42 AM
Subject: Re: [Qt-interest] Reference Error: google is not defined

There are quite a few reasons it won't work. Let me just summarize this for you:

1. ECMAScript is a standard for a programming language, but as any
programming language, a lot of things are implementation defined,
which are obviously not part of the standard.
2. The Google JS code you wish to use, obviously uses atleast the
XMLHttpRequest. This object is defined in the HTML DOM, and is not a
part of Javascript, but a part of the implementation of Javascript for
web browsers. There is no reason why it should be supported in any
other ECMAScript implementation.
3. Don't expect the Qt Script engine to understand <script> tags. It
is not an HTML parser, it understands JavaScript only.
4. JavaScript and ECMAScript can be used interchangeably, but the
implementation for a browser environment is not exactly what one would
call 'JavaScript'. Again, it is an implementation.

Regards,
rohan

On Thu, Mar 3, 2011 at 5:46 PM, Karthick <sppkarthick at gmail.com> wrote:
> Hi All,
> I am trying to execute javascript through Qt script.
> My javascript contains code for google maps API.
> when i ran it through Qt script, it gives an Error like "Reference
> Error: google is not defined".
>
> I think the problem is the
> <script type="text/javascript" src="http://maps.google.com/maps/api/js?
> sensor=false">
> </script>
> line from the html file. If I use the <> brackets, the java script
> engine won't compile it, and if I omit the line, the program gets the
> "google not found" error...
>
> Please anyone help me how to call the URL inside <script> tag in
> javascript which is been executed by Qt script?
>
> NOTE:
> Here, I have included JS and Qt script file.
> JS file:
> function calcRoute() {
>  var directionsService = new google.maps.DirectionsService(
> ); //
> getting error here."google not defined"
>  var start = "chennai";
>  var end = "bangalore";
>  var request = {
>    origin:start,
>    destination:end,
>    travelMode: google.maps.DirectionsTravelMode.DRIVING
>  };
>  directionsService.route(request, function(resp, status) {
>    if (status == google.maps.DirectionsStatus.OK) {
>        return resp;
>    }
>
>  });
> }
>
> QtScript file:
> #include <QApplication>
> #include <QtScript>
> #include <QScriptEngine>
>
> int main(int argc, char **argv)
> {
>   QApplication app(argc, argv);
>   QScriptEngine engine;
>   QScriptValue global = engine.globalObject();
>
>   QString fileName1 = "direction.js";
>   QFile scriptFile1(fileName1);
>   if(!scriptFile1.open(QIODevice::ReadOnly))
>   {
>      qDebug() << "error";
>   }
>   QTextStream stream1(&scriptFile1);
>   QString contents1 = stream1.readAll();
>   scriptFile1.close();
>   QScriptValue result1 = engine.evaluate(contents1, fileName1);
>   if(engine.hasUncaughtException()) {
>       int line = engine.uncaughtExceptionLineNumber();
>       qDebug() << "uncaught exception at line" << line << ":" <<
> result1.toString();
>   }
>   QScriptValue val1 = global.property("calcRoute");
>   QScriptValueList args1;
>   QScriptValue final_res1 = val1.call(QScriptValue(), args1);  //
> calling JS function calcRoute with no args
>   qDebug() << "The result is: " << final_res1.toString();
>   return app.exec();
> }
>
> --
> Regards,
> Karthick
>
>
>
>
>
>
>
>
>
>
>
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>
>
_______________________________________________
Qt-interest mailing list
Qt-interest at qt.nokia.com
http://lists.qt.nokia.com/mailman/listinfo/qt-interest



      



More information about the Qt-interest-old mailing list