title: Variable outside event handler's scope
body: <p>I am having a Javascript kerfuffle that goes beyond my knowledge of the language.</p>
  
  <p>I am creating a custom plugin for CKEditor. The plugin is an IFrame that opens in a DIV generated by CKEditor. </p>
  
  <p>Inside the IFrame, I am displaying a number of images. If the user selects one of those images, the HTML code necessary to display that image is inserted into the CKEditor. </p>
  
  <p>Dynamically doing this is where I'm stuck. I am connecting to the CKEditor instance from within the Iframe like so:</p>
  
  <pre><code>var CKEDITOR = window.parent.CKEDITOR;    
  </code></pre>
  
  <p>CKEditor offers an "OK listener" that triggers when the OK button (rendered by CKEDitor) is clicked. That OK listener is outside the IFrame. </p>
  
  <p><strong>Defining an OK listener that works with static values works:</strong> </p>
  
  <pre><code>var okListener = function(ev) {
     this._.editor.insertHtml('&lt;img class="symbol" src="my_static_symbol.gif"&gt;');
     CKEDITOR.dialog.getCurrent().removeListener("ok", okListener);
  };
  
  // Assign OK listener
  CKEDITOR.dialog.getCurrent().on("ok", okListener);
  </code></pre>
  
  <p><strong>But I don't know my return value yet when I assign the OK listener, so I would need to do something like this:</strong></p>
  
  <pre><code>var okListener = function(ev) {
     this._.editor.insertHtml('&lt;img class="symbol" src="'+my_dynamic_value()+'"&gt;');
     CKEDITOR.dialog.getCurrent().removeListener("ok", okListener);
  };
  
  // Assign OK listener
  CKEDITOR.dialog.getCurrent().on("ok", okListener);
  </code></pre>
  
  <p>But this doesn't work because <code>my_dynamic_value</code> is outside the function's scope at the time it is fired by CKEditor's "OK" button.</p>
  
  <p>I could of course, every time the user selects a different image in the list, update the <code>okListener</code> function and hard-code the current value using <code>eval</code> into it. But that feels like a horrible waste of resources to me. </p>
  
  <p><strong>Is there some scope trick I can use so I can access stuff from my Iframe from within okListener()?</strong></p>
  
  <p>I hope this is clear enough. If it isn't, comment and I will clarify.</p>