如何在页面加载之前在jQuery Mobile中禁用Ajax?

时间:2011-12-30 21:23:15

标签: javascript jquery html ajax jquery-mobile

在我的移动网站上。我一直在尝试加载Adsense移动广告,但在页面加载后它们会继续占用整个页面。

我确实知道如果我禁用ajax,页面就可以加载广告。这仅适用于我加载的第二页,因为我单击了带有标记的链接...

data-ajax="false"

这使得下一页加载完美。

问题:加载的第一个页面将被adsense广告覆盖,因为启用了ajax(我认为)。

基本上我的页面的第一部分看起来像这样......

<html>
<head>

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0rc3/jquery.mobile-1.0rc3.min.css" />
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0rc3/jquery.mobile-1.0rc3.min.js"></script>
<script language="text/javascript">

      $(document).bind("mobileinit", function () {

            $.mobile.ajaxEnabled = false;

      });

</script>
</head>
<body>

    <div data-role="header">
        <h1>Angry Birds Cheats</h1>
    </div>



    <div data-role="content">

<div>
    <script type="text/javascript"><!--
  // XHTML should not attempt to parse these strings, declare them CDATA.
  /* <![CDATA[ */
  window.googleAfmcRequest = {
    client: '',
    format: '',
    output: '',
    slotname: '',
  };
  /* ]]> */
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_afmc_ads.js"></script>
</div>

我确实尝试在代码中禁用ajax,但我认为这不是因为广告仍占用整个页面...

我在想,也许我可以在某个页面启动访问者并将其重定向到非ajax的页面。

4 个答案:

答案 0 :(得分:60)

检查绑定到mobileinit事件的文档:http://jquerymobile.com/demos/1.0/docs/api/globalconfig.html

特别是这一点:

  

因为mobileinit事件在执行时立即触发,   你需要在加载jQuery Mobile之前绑定你的事件处理程序。

以下是绑定到mobileinit事件的正确格式:

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0rc3/jquery.mobile-1.0rc3.min.css" />
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script type="text/javascript">
$(document).bind("mobileinit", function () {
    $.mobile.ajaxEnabled = false;
});
</script>
<script src="http://code.jquery.com/mobile/1.0rc3/jquery.mobile-1.0rc3.min.js"></script>

首先是jQuery Core(所以.bind()将可用),然后是mobileinit事件处理程序,然后是jQuery Mobile js文件(这是最后一次,因此mobileinit的事件处理程序将在事件被触发之前设置。)

您可以通过在函数中放置mobileinit来测试当前alert事件处理程序是否未触发。

答案 1 :(得分:5)

更新的jQuery Mobile文档在这里: http://jquerymobile.com/test/docs/api/globalconfig.html

与其他jQuery项目(如jQuery和jQuery UI)不同,jQuery Mobile一加载就会自动应用许多标记增强功能(早在document.ready事件触发之前)。这些增强功能基于jQuery Mobile的默认设置应用,这些设置旨在用于常见场景。如果需要更改设置,则可以轻松配置。

mobileinit事件

当jQuery Mobile启动时,它会触发文档对象上的mobileinit事件。要覆盖默认设置,请绑定到mobileinit。

$(document).on("mobileinit", function(){
  //apply overrides here
});

由于mobileinit事件是立即触发的,因此您需要在加载jQuery Mobile之前绑定事件处理程序。按以下顺序链接到JavaScript文件:

<script src="jquery.js"></script>
<script src="custom-scripting.js"></script>
<script src="jquery-mobile.js"></script>

您可以通过使用jQuery的$ .extend方法扩展$ .mobile对象来覆盖默认设置。

$(document).on("mobileinit", function(){
  $.extend(  $.mobile , {
    foo: bar
  });
});

或者,您可以使用对象属性表示法设置它们。

$(document).on("mobileinit", function(){
  $.mobile.foo = bar;
});

答案 2 :(得分:1)

了解jquery mobile ajax行为的有用页面

http://jquerymobile.com/test/docs/pages/page-links.html

要启用动画页面过渡,所有指向外部页面的链接(例如products.html)都将通过Ajax加载。

指向其他域或具有rel =“external”,data-ajax =“false”或目标属性的链接将不会加载Ajax。相反,这些链接将导致整页刷新而没有动画过渡。两个属性(rel =“external”和data-ajax =“false”)具有相同的效果,但是在链接到另一个站点或域时应使用不同的语义:rel =“external”,而data-ajax =“ false“对于简单地选择域中的页面通过Ajax加载非常有用。

答案 3 :(得分:0)

在Anchor标记上使用data-ajax =“false”禁用每页解决方案效果很好