Execute javascript when page shows

Execute javascript when page shows

Hi,
 
I need to create some JQM pages that execute javascript automatically when pages show. I am having trouble to make them work. e.g.  The following page:
 
 
When "Go to Nav 2" is clicked I expect to see "Nav 2" in the alertbox, but "Nav 1" shows instead. If I go to nav2.html directly, it does alert "Nav 2".
 
Thanks