[Qt-interest] FW: addToJavaScriptWindowObject for JS in HTML?

Tony Rietwyk tony.rietwyk at rightsoft.com.au
Tue Oct 13 01:18:53 CEST 2009


Please send replies to the list...

Glad to see you fixed your problem.  

getElementById will only report HTML objects that have the id attribute set
in the DOM.  Javascript objects are not part of the DOM, so I would not
expect it to work.  

Tony.


-----Original Message-----
From: pcolby at gmail.com [mailto:pcolby at gmail.com] On Behalf Of Paul Colby
Sent: Tuesday, 13 October 2009 09:18
To: Tony Rietwyk
Subject: Re: [Qt-interest] addToJavaScriptWindowObject for JS in HTML?


Hi Tony, thanks for the input.

I actually did set the function on the onload event.  In fact, in an
attemp to make sure that timing was not the issue, I've even set a
repeated JavaScript timer (via setInterval) that re-called the
function once every second, and still the same results.

But, in completely typical fashion, while cutting everything down to
simplest possible example in order to post some actual sample code,
I've found the error!! :(

My JavaScript function looked like this:

  function init() {
    alert(myObj);
    alert(document.getElementById("myObj"));

    document.getElementById('version').innerHTML = "loading";
    var myObj =document.getElementById('myObj');
    if (myObj ==null)
      document.getElementById('version').innerHTML="not found!";
    else
      document.getElementById('version').innerHTML= myObj.version;
  }

Can you see the problem?  About half way down the function, I've
declared myObj as a local variable!!  So of course the local myObj is
null at the start of the function! I feel so silly :|

So anyway after commenting out the latter lines,  alert(myObj)  is now
working.  Just out of curiosity, should the:

    alert(document.getElementById("myObj"));

work too?  It still reports "undefined".

Thanks again :)

paul.

On Mon, Oct 12, 2009 at 6:47 PM, Tony Rietwyk
<tony.rietwyk at rightsoft.com.au> wrote:
> Hi Paul,
>
> If those alerts are in the page itself, it might indicate that the signal
is
> only received *after* the page is loaded.  So only javascript in events
will
> see the Qt object.
>
> Try putting them in a <body onload> handler.
>
> Hope that helps!
>
>
>
>> -----Original Message-----
>> From: qt-interest-bounces at trolltech.com
>> [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Paul Colby
>> Sent: Monday, 12 October 2009 17:45
>> To: qt-interest
>> Subject: [Qt-interest] addToJavaScriptWindowObject for JS in HTML?
>>
>>
>> I can't figure this out... there must be something I'm missing.
>>
>> I've used QWebFrame::addToJavaScriptWindowObject quite a bit, and find
>> it very useful.  But now I'm trying to use it slightly differently,
>> but to my surprise I cannot get it to work.
>>
>> I have a basic QObject-derived class that implements a single
>> read-only Q_PROPERTY called "version".
>>
>> So if I expose that object under the name "myObject", then this works:
>>
>> QVariant
>> result=webView.page()->mainFrame()->evaluateJavaScript(QString
>> ::fromAscii("myObject.version"));
>>
>> However, if I load a HTML page that contains the following JavaScript:
>>
>> alert(myObject); // null.
>> alert(document.getElementById("myObject")); // undefined.
>>
>> I get "null" and "undefined" respectively.  Should this be the case?
>>
>> I know about QWebFrame::javaScriptWindowObjectCleared - I'm calling
>> addToJavaScriptWindowObject there, and in about 20 other places /
>> signals just be sure... but that none of them help :(
>>
>> Also, I've overridden QWebPage::javaScriptAlert, and there, as the
>> "null" and "undefined" messages are coming though, I can see that the
>> associated QWebFrame parameter *does* has have my object (ie
>> evaluateJavaScript finds it just fine).
>>
>> So, am I missing something?  Is HTML-embedded JavaScript meant to be
>> able to see objects exposed by addToJavaScriptWindowObject?  or are
>> they only meant to be visible to explicit evaluateJavaScript calls?
>>
>> Thanks :)
>>
>> paul





More information about the Qt-interest-old mailing list