What does javascript "Invalid write to global property XXXX" mean?



  • I'm trying to import a JavaScript HTML parser from "here":http://ejohn.org/blog/pure-javascript-html-parser/#postcomment so I can parse my text back from a XMLHttpRequest. Unfortunately I get the error
    bq. Invalid write to global property "HTMLParser"

    What does this mean, and how can I get around it?

    Has anyone else used this parser, or got other suggestions on how to parse HTML if the responseXML returns Null. Its a bit sad that you can't use the Xpath parser used in the XML model on arbitrary HTML.

    Note, the reason I need to do this is that in some cases XMLHttpRequest responseXML returns null - and I'd like to parse the content. This problem is discussed on Nokia Developer "here":http://www.developer.nokia.com/Community/Discussion/showthread.php?232839-Qt-Quick-and-DOM-doc.responseXML-returns-null, I'm not getting null returned from my XMLHttpRequest for XML.



  • I don't know exactly the meaning....but usually it happens when you forgot to define a variable, using it without "var"



  • Hi,

    QML doesn't allow the global object to be modified (see http://doc.qt.nokia.com/4.7-snapshot/qdeclarativejavascript.html#qml-javascript-restrictions for further explanation of this restriction). Most likely it is lines like:

    @var HTMLParser = this.HTMLParser = ...@

    which are causing the issue (where this is referring to the global object). It should be possible to tweak the js to get things working (I would start by defining your own "var HTMLParser" at the top of the file, though I haven't tested this).

    Regards,
    Michael



  • Hi Michael, dmcr

    Thank you very much for your responses. I did actually try adding "var HTMLParser;" and "var HTMLParser = new HTMLParser (etc)" at the top of the page and got same error. I suspect its a chicken and egg thing.

    Unfortunately my JavaScript is weak. Any more direct help in porting much appreciated, although not expected. I'm reading up and I'm sure I'll be able to get this working at some point. If not I'll just write a layer over the Qt C++ parsers, although I'd prefer not to.

    Regards
    Hamish



  • Hi,

    You should be able to add the following to the top of your page

    @var HTMLParser;
    var HTMLtoXML;
    var HTMLtoDOM;@

    and then change the existing library lines that look like

    @var HTMLParser = this.HTMLParser = ...@

    to

    @HTMLParser = ...@

    (there should be 3 library lines that need changing, one for each of the vars added to the top of the file)

    Regards,
    Michael



  • Thanks. Sadly did not work (same error)



  • Hmm, that worked for me (using Qt 4.7). How are you including the JS in QML (in a separate file that is imported?). Can you paste the exact errors you are seeing?

    Regards,
    Michael



  • All my JS is imported into the QML using:

    @import "libraryfunctions.js" as AccessLibrary@

    I've tried adding the htmlparser.js to libraryfunctions.js in two ways:

    Just copy pasting the code

    include the library in it:

    @var statusthingy=Qt.include("htmlparser.js")
    console.log("STATUSTHINGthingy:", statusthingy.status);
    console.log("EXCP:", statusthingy.exception);@

    In both cases I get the exception.
    @EXCP: Error: Invalid write to global property "HTMLParser"@

    When using the approach where I just add htmlparser into my existing javascript I can see that the error occurs here:
    @> };
    HTMLtoXML=this.HTMLtoXML = function( html ) {@

    Note, did I do the HTMLtoXML change correct?



  • Hi,

    You'll also need to get rid of the this.HTMLtoXML on that line, i.e. it should end up as something like:

    @HTMLtoXML = function( html ) {@

    (with var HTMLtoXML; at the top of the file)

    Regards,
    Michael



  • Michael, thank you very much. That works.

    Just in case anyone else wants to use this library, note that you can use the sax parser and XML serialiser but you can't use the DOM builder or other methods. Sadly these depend on DOM implementation which doesn't exist in QML javascript. Still, SAX alone is helpful.



  • Hi,

    I have been trying to port http://ejohn.org/files/htmlparser.js to my QML application. Thanks for this thread.

    I have a problem, when I try to use HTMLtoXML. With the examples given by the original author it works fine.. for instance,

    HTMLtoXML("<p><b>Hello") == '<p><b>Hello</b></p>'

    However, when i try to use this function with the html text fetched from some xyz website (by using XHttpRequest) this function never returns. To make sure, I tried the following example

    htmlToXML("<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">")

    The input here is the first line of the output text returned from XhttpRequest call.

    Have you tried this use case ?

    -CK



  • Hi Chanderk,
    I did not a problem with that line, but I did not attempt to address it. I simply stripped the whole of the line using a regular [removed]after which everything worked fine)
    If you work out why the parser is failing then please let me know so we can have a more robust solution.
    Regards
    Hamish


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.