Disregard the $.mobile.changePage(url)
comment underneath your post; it is not useful.
Several Things:
I recommend spending some time with jQuery.Mobile Documentation. There are a lot of quirks specific to jQueryMobile that you would not guess automatically.
As the commenter stated (correctly), don't use
$(document).ready()
with jQueryMobile. It's not that it can't be used, it's just that it's not recommended, and you will likely encounter surprises if you do.What you want to use to initialize your code varies depending on what you are doing. Most likely you'll want to use
$.mobile.pageInit
.from the docs
Description: Triggered on the page being initialized, after initialization occurs.
jQuery( ".selector" ).on( "pageinit", function( event ) { ... } )
We recommend binding to this event instead of DOM ready() because this will work regardless of whether the page is loaded directly or if the content is pulled into another page as part of the Ajax navigation system.
If you are just trying to go to a different page on the click, you would be better off not adding any javascript, and just letting jQuery take care of the page handling. If it is dynamic, then you would use (as was suggested)
$.mobile.changePage(url)
. To make it appear inline - in either case - you would usepersistent footers and headers
(again - see the docs).If you insist on doing it the way you have it (for whatever reason - there could be many), then you need to call
preventDefault()
when clicking on a link.Return false
will not work reliably to prevent the default behavior, and given that jqueryMobile often involves many different elements when handling events, it's not a bad idea to usestopPropagation()
as well. However, you'd be better off removing the anchors altogether and using something likedata-ajax-href
to represent the page jQuery is going to load. Any page you have a link to anywhere in your document, jQueryMobile will (unless specifically instructed not to) preload after the firstpageInit
. This can be good at times, not so much so at others.
Basically, I can't advise specific to your situation (more so anyway) without a clearer understanding of what you want to achieve, and code that closer conforms to the documented standards. Everything you could possibly want is in the docs, and they are considerably more readable than almost any other Javascript library API that I've ever seen. Check 'em out.