title: #ifndef in javascript
body: <p>I am looking for a solution to only define a function once in Javascript using something exactly like #ifndef in compiled languages. I found a couple of libraries that were supposed to mimic this functionality but they didn't work. </p>
  
  <p>I am working with MVC 3 Razor and have defined some html helpers do put what are essentially user controls onto the page.</p>
  
  <p>Each control has a set of javascript functions that define specific functionality for that control, so herein lies the issue: the functions get defined multiple times when the helper is called multiple times on a single page. </p>
  
  <p>I am hoping to find a way to keep the very small amount of javascript defined within the helper and not have to divide all of the javascript for each of these small helpers in a separate file.</p>
  
  <p>Sample:</p>
  
  <pre><code>@helper CmsImage(int id)
  {
    var Html = ((System.Web.Mvc.WebViewPage)WebPageContext.Current.Page).Html;
  
    &lt;text&gt;
      &lt;input type="button" class="editor_function" style="display: none;" onclick="editImage(@id); return false;" /&gt;
      &lt;script&gt;
          function editImage(id) {
              $('#alt_text' + id).attr('value', $('#' + id).attr('alt'));
              $('#image_url' + id).attr('value', $('#' + id).attr('src'));
          }
  
          function saveImage(button, id) {
              $(button).parent().parent().removeClass('color-yellow').addClass('color-red');
              $(button).parent().siblings('div.widget-content').html('&lt;img alt="' + $('#alt_text' + id).val() + '" src="' + $('#image_url' + id).val() + '" id="' + id + '" /&gt;');
          }
          #endif 
      &lt;/script&gt;
      Image Url:
      &lt;input type="text" id="image_url@{id.ToString();}" /&gt;&lt;br /&gt;
      Alt Text:
      &lt;input type="text" id="alt_text@{id.ToString();}" /&gt;&lt;br /&gt;
      &lt;input type="button" value="save" onclick="saveImage(this, @{id.ToString();});" /&gt;
      @Html.Raw(GetCurrentContent(id))
    &lt;/text&gt;
  }
  </code></pre>
  
  <p>The above doesn't work in the browser, if gives me the error: '48: Unrecognized token ILLEGAL'</p>