开发者

How to prevent jQuery Mobile from making ajax calls when dynamically injecting html pages

开发者 https://www.devze.com 2023-02-14 01:13 出处:网络
Instead of using ajax calls, I create and inject pages into the $.mobile.pageContainer. Dynamically creating jQuery Mobile pages using jQuery Templates

Instead of using ajax calls, I create and inject pages into the $.mobile.pageContainer. Dynamically creating jQuery Mobile pages using jQuery Templates

When I want to access a page with a hash tag (one that is generated in my onReady function), jQuery mobile tries to make an ajax call. It fails. When my onReady function is called, I have to check the url and call $.mobile.changePage() to make it show up.

var loc = window.location.href;
var loc = loc.split('#').pop();
if (loc !== "http://lift.pageforest.com/") {
    $.mobile.changePage(loc, 'pop', false, true);
}

That's all fine, but jQuery Mobile has still made a failed ajax call resulting in an error thrown to the console as well as a big error div shown to the user.

I tried overriding the $.mobileinit function ajaxEnabled() to false because I will never use ajax. http://jquerymobile.com/demos/1.0a3/#docs/api/globalconfig.html Unfortunately that created a whole bunch of other problems.

Why does开发者_开发百科 jQuery mobile automatically assume that I want to use ajax, and I will not generate any content in my own onReady function? How do I work around this?

Reposted from here: http://forum.jquery.com/topic/how-to-disable-automatic-ajax-calls-when-dynamically-creating-pages


Can you use event.preventDefault();?

http://api.jquery.com/event.preventDefault/

Or if you set rel="external" and a JQ class selector in your link it will prevent the default internal linking.

<a href="#mylink" class="hash-link" rel="external">Link</a>

<script>  
$('.hash-link').click(function() {

   var loc = window.location.href;
   var loc = loc.split('#').pop();
   if (loc !== "http://lift.pageforest.com/") {
        $.mobile.changePage(loc, 'pop', false, true);
    }

});
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消