Programming

In Lectora Online how can I run an Action Group from JavaScript?

For Lectora Online do this to run Action Groups from JavaScript.

1. Add an On Show - Run JavaScript Action to your page. Name it "Define Lectora Group Objects".  This is going to create JavaScript variables that map to the Action Group that have a prefix of LGO in their name.  (ie. LGO_MyActionGroup).

This is the code to add to the Run JavaScript action:

// set whatever prefix you want to use for object names that will map to their HTML object
var objectPrefix = "LGO"; // Lectora Group Objects

var chlds = document.head.childNodes;
for (var i=0;i<chlds.length;i++)
{
    if (chlds[i].tagName && chlds[i].tagName.toLowerCase().indexOf('script')>-1 && 
        chlds[i].innerHTML && chlds[i].innerHTML.indexOf('trivWeb20Popups')>-1 )
    {
        var re = new RegExp("var\\s*([^\\s]*).*//("+objectPrefix+"[^\\s]*)",'g');
        var s = chlds[i].innerHTML;
        s.replace(re, function(match, g1, g2) { 
            window[g2] = window[g1];
            window[g2].runme = function(){this.issueActions(1001);}; 
        });
    }
}

2. Name Action Groups you want to run with the prefix you used. (ie. LGO_MyActionGroup)

3. In Run JavaScript actions you can now run your action groups by specifying the <name>.runme();

LGO_MyActionGroup.runme()

See the example below:

 

This only works in Lectora Online.