title: How to run JavaScript Code when an issue is displayed in JIRA
body: <p>I am currently developing a JIRA 7 server add-on, I am stuck and I'd be grateful for any help I can get. I am new to both JIRA and JavaScript, so forgive me if I fail to see the obvious solution :-)</p>
  
  <p>My Add-On includes a web panel which needs some JavaScript to function. I intend the JavaScript to register a click handler that triggers a dialog. Currently it looks something like this:</p>
  
  <pre><code>AJS.toInit(function() {
  
    var buttonClickHandler = function() {
      AJS.dialog2("#myDialogId").show();
    };
  
    AJS.$("#myButtonId").click(buttonClickHandler);
  }
  </code></pre>
  
  <p>The actual ids of the dialog and the button depend on the issue which is displayed. Obiously I want this code to run when my dialog2 and my button are present in the DOM, which is everytime that I navigate to an issue (not just the first time I navigate to <em>any</em> issue). Unfortunately I do not know how to do that. I have tried the following approaches, neither of which worked:</p>
  
  <ol>
  <li><p>bind my JavaScript file to the atl.general context. If I do that the script runs the first time I load a page that belongs to the atl.general context, i.e. the dashboard, any issue, etc., but when I navigate to another issue the script does not run again.</p></li>
  <li><p>bind my JavaScript file to a custom context and use that in my template. If I do that the script runs the first time that an issue is displayed which has that web panel, but not run again, when I navigate to a different issue.</p></li>
  </ol>
  
  <p>I figured I could find a solution based on jira events, i.e. register a function as an event handler using JIRA.bind() and put my code in there. However that would require an event that is triggered whenever I want my code to run, i.e. whenever an issue is displayed. I tried <code>JIRA.Events.NEW_CONTENT_ADDED, function(e, context, reason) {...})</code>, but the event handler was never called (I checked with console.log() statements). I have no idea which other Jira events exist which I might use to my advantage, nor can I find any documentation on it.</p>