This is a wild guess but I think I know what is wrong in your case.
To understand this situation you need to understand how jQuery Mobile works. It uses ajax to load other pages.
First page is loaded normally. Its HEAD and BODY is loaded into the DOM, and they are there to await other content. When second page is loaded, only its BODY content is loaded into the DOM. So when you open your page directly and your javascript is placed inside a HEAD that javascript will execute successfully but if you are opening other html file and its javascript is in head it will be discarded as only BODY will be loaded.
If you want to find more about this problem and how to solve it (+ working solutions) take a look at my other answer: Why I have to put all the script to index.html in jquery mobile