[英]How To Disable Ajax In jQuery Mobile Before Page Load?
在我的移動網站上。 我一直在嘗試加載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的頁面。
查看綁定到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
的事件處理程序)。
您可以通過在函數中放置alert
來測試當前的mobileinit
事件處理程序是否未觸發。
更新的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;
});
一個有用的頁面來理解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加載非常有用。
禁用每頁解決方案可以很好地使用Anchor標記上的data-ajax =“false”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.