calling a javascript action from a javascript action

  • a beginners question .)

    if i defined some javascript action with
    <action name="test" type="Javascript"><![CDATA[ ... ]></action>

    how do i call it from another javascript action?

    i guess krpano.call("test();"); would do it,
    but like this handling arguments is a headache (as everything must get packed into a string)

    is there a more elegant way?

    (sadly krpano.test(); doesnt work)

  • Hi,

    it would be possible to a make a global/init Javascript action where all Javascript functions will be created and added to krpano at once,
    e.g. like this (note - the autorun=preinit):

    this way you can call the other JS actions from the other JS actions there.

    The actions added this way will be also callable from normal xml actions, e.g. just:

    Code
    <action name="test_js_actions">
        test1();
        test2();
    </action>

    Best regards,
    Klaus

  • wheee.. thanks... that is very nice :
    almost there... without arguments it works,
    but still strugging with functions using arguments ...

    for example i have this:

    Code
    <action name="test" type="Javascript"><![CDATA[
    args.shift(); krpano.trace(0,args.join(""));
    ]]></action>

    by changing this to an init funktion like this:

    Code
    <action name="test_init" type="Javascript" autorun="preinit"><![CDATA[
    krpano.actions.test = function() {
    args.shift(); krpano.trace(0,args.join(""));
    }
    ]]></action>

    i can call test() from any js action with:

    Code
    krpano.actions.test("hello");

    and from action script with:

    Code
    test("hello");

    for both the call works, but
    somehow the args array is not recognized anymore...
    i couldn't find the solution for this :/

    best, index

  • Hi,

    don't overthink that problem *wink* - just use normal Javascript arguments.
    That 'args' is for the <action> itself.

    E.g.

    Code
    <action name="test_js_actions">
      test1('call from xml', 1);
      test2('call from xml', 2);
    </action>

    Instead of defining the arguments at the function, you could also use the Javascript native arguments object.

    The only thing to consider is the type of the arguments, when called from xml the argument might be a string by default.
    E.g. you want expect a Number as type, then always convert the argument - e.g. just by - arg = Number(arg).

    Best regards,
    Klaus

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!