title: jQuery Mobile - Auto open dialog
body: <p>This should be an easy question, but there seems to be something strange going on. I'm using Django, and want to have a jQuery mobile dialog open with the current messages on any page view. After closing the dialog, the window should return the the page that was about to be viewed. So really the issue is how to open a dialog on page load, and then show the page.</p>
  
  <p>This (any many variations on it) does not work:</p>
  
  <pre><code>{% if messages %}
  &lt;section class="dialog" id="messagesDialog" data-role="dialog" data-theme="a"&gt;
  &lt;header class="okay" data-role="header"&gt;&lt;h1&gt;This is a title&lt;/h1&gt;&lt;/header&gt;
  &lt;div data-role="content"&gt;
      &lt;ul&gt;
          {% for message in messages %}
          &lt;li{% if message.tags %} class="{{ message.tags }}"{% endif %}&gt;{{ message }}&lt;/li&gt;
          {% endfor %}
      &lt;/ul&gt;
  &lt;/div&gt; {# content #}
  &lt;/section&gt;
  
  &lt;script type="text/javascript"&gt;
  $(document).one('pageinit', function(event) {
      $.mobile.changePage("#messagesDialog", 'pop', true, true);
  });
  &lt;/script&gt;
  {% endif %}
  </code></pre>
  
  <p>But this does:</p>
  
  <pre><code>{% if messages %}
  &lt;section class="dialog" id="messagesDialog" data-role="dialog" data-theme="a"&gt;
  &lt;header class="okay" data-role="header"&gt;&lt;h1&gt;This is a title&lt;/h1&gt;&lt;/header&gt;
  &lt;div data-role="content"&gt;
      &lt;ul&gt;
          {% for message in messages %}
          &lt;li{% if message.tags %} class="{{ message.tags }}"{% endif %}&gt;{{ message }}&lt;/li&gt;
          {% endfor %}
      &lt;/ul&gt;
  &lt;/div&gt; {# content #}
  &lt;/section&gt;
  
  &lt;script type="text/javascript"&gt;
  $(document).one('pageinit', function(event) {
      $.mobile.changePage("WhatHeck?!?!"); // This will return a 500 error from the server
      $.mobile.changePage("#messagesDialog", 'pop', true, true);
  });
  &lt;/script&gt;
  {% endif %}
  </code></pre>
  
  <p>So, is this a known issue, or something I'm missing? The only way I can get the page to pop the dialog and then show the correct page after closing it, is by calling <code>$.mobile.changePage()</code> to cause an error before changing to the dialog.</p>
  
  <p>===Added===</p>
  
  <p>No Error, no dialog, correct page:</p>
  
  <pre><code>$("section@[data-role=page]").one('pagechange', function(event) {
      //$.mobile.changePage("WhatHeck?!?!");
      $.mobile.changePage("#messagesDialog",{
          transition : 'pop',
          reverse    : false,
          changeHash : true
      });
  });
  </code></pre>
  
  <p>No Error, opens dialog, wrong page:</p>
  
  <pre><code>$(document).one('pagechange', function(event) {
      //$.mobile.changePage("WhatHeck?!?!");
      $.mobile.changePage("#messagesDialog",{
          transition : 'pop',
          reverse    : false,
          changeHash : true
      });
  });
  </code></pre>
  
  <p>500 error from server requesting /WhatHeck?!?! (expected), opens dialog, correct page:</p>
  
  <pre><code>$(document).one('pagechange', function(event) {
      $.mobile.changePage("WhatHeck?!?!");
      $.mobile.changePage("#messagesDialog",{
          transition : 'pop',
          reverse    : false,
          changeHash : true
      });
  });
  </code></pre>
  
  <p>No error, opens dialog, wrong page:</p>
  
  <pre><code>$(document).one('pagechange', function(event) {
      $.mobile.changePage($("section@[data-role=page]"));
      $.mobile.changePage("#messagesDialog",{
          transition : 'pop',
          reverse    : false,
          changeHash : true
      });
  });
  </code></pre>
  
  <p>Error: "Uncaught TypeError: Cannot call method '_trigger' of undefined", No dialog, correct page:</p>
  
  <pre><code>$(document).one('pagechange', function(event) {
      $.mobile.changePage($("section@[data-role=page]",{
          changeHash : true
      }));
  
      $.mobile.changePage("#messagesDialog",{
          transition : 'pop',
          reverse    : false,
          changeHash : true
      });
  });
  </code></pre>
  
  <p>No Error, opens dialog, wrong page:</p>
  
  <pre><code>$(document).one('pagechange', function(event) {
      $.mobile.changePage("#Home");
      $.mobile.changePage("#messagesDialog",{
          transition : 'pop',
          reverse    : false,
          changeHash : true
      });
  });
  </code></pre>
  
  <p>Actually works!</p>
  
  <pre><code>$(document).one('pagechange', function(event) {
      $.mobile.changePage("#Home", {
          allowSamePageTransition : true
      });
  
      $.mobile.changePage("#messagesDialog",{
          transition : 'pop',
          reverse    : false,
          changeHash : true
      });
  });
  </code></pre>
  
  <p>Also works (For any page):</p>
  
  <pre><code>$(document).one('pagechange', function(event) {
      $.mobile.changePage($("section@[data-role=page]"), {
          allowSamePageTransition : true
      });
  
      $.mobile.changePage("#messagesDialog",{
          transition : 'pop',
          reverse    : false,
          changeHash : true
      });
  });
  </code></pre>
  
  <p>So the solution to my problem, is the answer to this question: What is happening in the jquery mobile state when the error page fails to load?</p>
  
  <p>So it seems like, when calling changePage from inside the pagechange event handler, the history had not yet been updated. Still doesn't make sense to me that I need to force it to reload the current page, then open the dialog. That's at least a better solution then expecting an error from the sever.</p>