<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet href="_c74_vig.xsl" type="text/xsl"?>
<vignette name="Communicating with Max from within jweb" package="Max" rankfactor="2">
  <h1>Communicating with Max from within jweb</h1>
  <p>
        In Max 7 <o>jweb</o> uses the Chromium Embedded Framework (CEF) for running websites within a Max box. What's new is that you can communicate with the JavaScript of the shown website. Please note that since CEF is in a seperate process, the communication is inherently asynchronous, and you can't rely on having sent and outputted messages within the same event.
    </p>
  <h2>Receiving messages from Max</h2>
  <p>
        The <b>bindInlet</b> function enables listening to messages being sent to the jweb object from within the JavaScript of the webpage. In order to do that you attach a callback function to certain messages as shown in the example below:

        <code language="javascript">
window.max.bindInlet('doSomething', function () {
    /*
     * trigger functionality without arguments
     *
     * message 'doSomething'
     */
});
        </code>
    </p>
  <p>
        If you'd like to send messages with arguments to the website simply send a list of space delimited values to the jweb object as shown below. You can either access these values as parameters in your listener callback function or use the array-like <i>arguments</i> object if the amount of parameters might vary.
        <code language="javascript">window.max.bindInlet('doSomethingWithArgs', function (a, b) {
    /*
     * if a fixed set of parameters doesn't meet
     * your needs you can use the array-like arguments
     * objects in order to gain access to the
     * incoming parameters
     *
     * message: doSomethingWithArgs 1 2
     */
});</code>
    </p>
  <h2>Sending messages to Max</h2>
  <p>
        Using the <b>outlet</b> function you can output messages from the JavaScript of the shown webpage to the jweb's first outlet.

        <code language="javascript">// output a string
window.max.outlet('foo');

// output a list
window.max.outlet('foo', 1, 2);

// output contents of array with prepended 'foo' message
var ar = [1, 2, 3, 4];
window.max.outlet.apply(window.max, ['foo'].concat(ar));</code>
    </p>
  <p>
        Additionally you can send messages to Max using the href attribute of an anchor tag if following a certain but simple syntax. Note that a message send this way will be outputted with a prepended 'maxmessage'.
        <code language="markup">&lt;a href="maxmessage:name/param1/param2"&gt;</code>
    </p>
  <h2>Interacting with Max dictionaries</h2>
  <p>
        Using <b>getDict</b> you can access the contents of a Max dict object from within a website's JavaScript.
        <code language="javascript">var nestedValue;

// access dictionary
window.max.getDict('dictName', function(dict) {
    // dict is a JS object, so you can do things like
    nestedValue = dict.a;
});</code>
    </p>
  <p>
        If you like to set the contents of a dictionary within Max you can use the the <b>setDict</b> function to do that.
        <code language="javascript">var obj = {
    a : '1',
    b : '2',
    c : '3'
};

window.max.setDict('dictName', obj);</code>
    </p>
  <seealsolist>
    <seealso name="jweb" type="refpage" module="kernel"/>
    <seealso name="maxurl" type="refpage" module="kernel"/>
  </seealsolist>
</vignette>
