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

Joe*_*Joe 29 html javascript ajax jquery jquery-mobile

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

我确实发现,如果我禁用ajax,页面会加载广告.这仅适用于我加载的第二页,因为我点击了带有标签的链接...

data-ajax="false"
Run Code Online (Sandbox Code Playgroud)

这使得下一页加载完美.

问题:加载的第一个页面将被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>
Run Code Online (Sandbox Code Playgroud)

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

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

Jas*_*per 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>
Run Code Online (Sandbox Code Playgroud)

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

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


blu*_*bie 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
});
Run Code Online (Sandbox Code Playgroud)

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

<script src="jquery.js"></script>
<script src="custom-scripting.js"></script>
<script src="jquery-mobile.js"></script>
Run Code Online (Sandbox Code Playgroud)

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

$(document).on("mobileinit", function(){
  $.extend(  $.mobile , {
    foo: bar
  });
});
Run Code Online (Sandbox Code Playgroud)

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

$(document).on("mobileinit", function(){
  $.mobile.foo = bar;
});
Run Code Online (Sandbox Code Playgroud)