简体   繁体   English

在Xul中传递任意JavaScript对象

[英]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): 这是我的第一个窗口(OBS 转储打印到控制台时的方式。 调试选项被打开):

<?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: 当第二个窗口加载时,它调用onload并打印:

arg = undefined arg =未定义

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: 我通常调用openDialog,因此可以不使用观察程序而直接传递一个或多个参数...但是从您尝试遵循的示例中,似乎您省略了这一行:

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. 另外,您可能需要确保在加载窗口之后才检查参数-在由窗口的onload属性调用的函数中检查参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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