简体   繁体   中英

Passing an arbritrary JavaScript object in Xul

I'm following this example to pass an object to a window, but when it as an argument it's with "undefined" value.

This is my first window (obs. dump is the way to print to console when debug options are turned on):

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>

<!DOCTYPE window SYSTEM "chrome://XulWindowArgTest/locale/XulWindowArgTest.dtd">

<window id="windowID" width="400" height="300"
    xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

    <script>
        <![CDATA[
            function onClickMe(event) {
                dump("begin\n");

            try {
                var args = {
                  param1: true,
                  param2: 42
                };
                args.wrappedJSObject = args;

                var watcher = Components.classes["@mozilla.org/embedcomp/window-watcher;1"].getService(Components.interfaces.nsIWindowWatcher);
                watcher.openWindow(null, "chrome://XulWindowArgTest/content/about.xul", "windowName", "chrome", args);

            } catch (e) {
                dump("error: " + e + "\n");
            }


                dump("end\n");
            }
        ]]>
    </script>

    <button label="Click me !" oncommand="onClickMe();" />

</window>

and my second window:

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>

<!DOCTYPE window SYSTEM "chrome://XulWindowArgTest/locale/XulWindowArgTest.dtd">

<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
    onload="onload()">

    <script>
        <![CDATA[
            function onload() {
                dump('arg = ' + window.arguments[0].wrappedJSObject + "\n");
            }
        ]]>
    </script>

    <label value="test" />

</window>

when the second window loads, it calls the onload and prints:

arg = undefined

Any idea how to fix it?

I usually call openDialog and therefore can just pass one or more parameters without using a watcher... but from the example you are trying to follow, it seems that you left out this line:

args.wrappedJSObject = args;

If you are trying to do it all inline perhaps you should be passing:

{wrappedJSOBject:["myarg"]}

Additionally, in your second window you are trying to print

window.arguments[0].wrappedJSObjectarg

when your example is referencing

window.arguments[0].wrappedJSObject;

I'm assuming the former error is an oversight and the latter is a typo.


Also, you might need to make sure that you aren't checking the arguments until after the window is loaded -- check them in a function called by the onload attribute of the window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM