title: Javascript execution in dynamically loaded dialogs
body: <p>I have som problems regarding getting javascript to fire correctly inside dialog.</p>
  
  <p>Below is the code from my scripts.js wich i currently use to load a dialog</p>
  
  <pre><code>function LoadDialog(url, title) {
      var DialogBox = $('&lt;div class="dialog-container"&gt;&lt;/div&gt;').dialog({ modal: true, autoOpen: false, resizeable: true }).dialog({ width: 500, height: 'auto' });
      DialogBox.html('&lt;div class="ajax-load"&gt;&lt;img src="/Content/loading.gif" alt="loading"&gt;&lt;/div&gt;').dialog({ buttons: null, title: 'Loading..' });
      DialogBox.dialog('open').load(url, function () {
          DialogBox.dialog({ title: title, Buttons: null, position: 'center', close: function (ev, ui) { $(this).dialog("destroy"); } });
      });
  }
  
  $(function () {
      $('.as-dialog').click(function (e) { LoadDialog($(this).attr('href'), $(this).attr('title')); return false; });
  });
  </code></pre>
  
  <p>Though no javascript code fires by default inside the dialog. For example i load a small form with unobtrusive validation into the dialog. No body, head etc, only a container div and the form itself. By default validation dont work and if i add event handlers for the form under script.js $(function(){}) it dont fire. If i add the following js references and codeblock to be loaded with the form it works with one problem:</p>
  
  <pre><code>&lt;script src="/Scripts/libs/jquery.validate.min.js"&gt;&lt;/script&gt;
              &lt;script src="/Scripts/libs/jquery.validate.unobtrusive.js"&gt;&lt;/script&gt;
              &lt;script type="text/javascript"&gt;
                  $(function () {
                      $('#form_Create').submit(function (e) {
                          e.preventDefault();
                      });
                  });
              &lt;/script&gt;
  </code></pre>
  
  <p>The non functioning regarding the code above is that validation will always work but if i open the dialog, close it, then open it again, the preventdefault will stop working and the form will post. Could this perhaps be a multiple load of script issue or something? Though since no 'doc ready' code works in dialog if placed in scripts.js (standard javascript functions work though) i dont really have another option. 
  Is there a better way to work with javascript inside dynamically loaded files or do i have to place the entire forms in the parent page to be able to work against them reliably? 
  Any input on how you should handle this?</p>