Question on dynamic pages

Question on dynamic pages

I have 2 questions, I am writing a webpage that has all its pages created dynamically.  I have 2 issues that I am not able to find an answer.  http://jsfiddle.net/A8B2F/3/

1. when the page loads, JQM seems to inject a generic empty page (this is what I get in the jsfiddle example  <div data-role="page" data-url="/_display/"> </div>) by the time document ready event is fired where I would insert the first page.  Is there an event or hook that I can subscribe to so that I can insert my first page content so JQM wouldn't inject an empty page. Or do I need to have my code generate the html markup before I include the jqm library?

2. I want to dynamically show a popup dialog from a template.  the button html is something like <a href="#MyPopup">, when would like to know where I can listen to event so that I can insert the page so it would show up.  Currently I listen to  $(document).on("pagebeforechange"), but it appears to be too late so the first time I click on the button, the dialog wouldn't appear but it would show up in subsequent clicks.  The jsfiddle for some reason doesn't work for the dialog showing, but it works in my code.

Any suggestion would be greatly appreciated.