2

I'm defining a Javascript function in my KRL global block that I want to call when the user clicks a link. Here are the relevant parts of the ruleset:

global {
  emit <|
    function clear_hold() {
      app = KOBJ.get_application("a421x26");
      app.raiseEvent("clear_hold");
    }

  |>;
}

rule add_link_to_clear_hold {
  select when pageview ".*"
  pre {
    clear_div = << <div id="clear_hold">
      <a href="javascript:clear_hold()">Clear Hold</a>
      </div> >>;
  }
  {
    append("body", clear_div);
  }

rule clear_the_hold {
  select when web clear_hold
  {
    replace_html("#clear_link", "<div id='clear_link'>Not on hold</div>");
  }
  always {
    clear ent:hold;
  }
}

When I click the link I get an error message that clear_link is not defined.

What do I need to do to call my javascript function?

Randall Bohn
  • 2,597
  • 2
  • 16
  • 20

2 Answers2

5

It is suggested to use the following name spacing method to attach JavaScript functions to the KOBJ object to avoid clashes with other apps the user might have running.

KOBJ.a60x33.clear_hold = function() { 
  KOBJ.log('...wohoo! You found me!'); 
}

The function can then be called with

KOBJ.a60x33.clear_hold();
Mike Grace
  • 16,636
  • 8
  • 59
  • 79
3

The function is defined inside the KRL closure, but I was calling from outside the closure. To make it visible outside I added it to KOBJ after defining the function

KOBJ.clear_hold = clear_hold;

Then to call it from the link:

href="javascript:KOBJ.clear_hold()
Randall Bohn
  • 2,597
  • 2
  • 16
  • 20