這個問題最終與JQuery Mobile AJAX有關的頁面轉換有關。 JQuery Mobile在第一次之後不會在其他頁面請求上加載文檔的頭部。
因此,當我將移動主機切換到桌面主機時,文檔的頭部不會加載以引入我的樣式。有幾個方式就是這種可以固定:
這樣只是關閉AJAX乾脆,並解決了這個問題,但你不能從中受益:
<form data-ajax="false">
這是一個辦法做到如果通過重定向
$.mobile.ajaxEnabled = false;
以上兩種解決方案,我支持可以工作:它有問題,而是提醒你,它不會通過事件jQuery Mobile的初始化工作後,如此反覆,你不能從中受益如果您必須使用onclick事件和事件處理函數,請先查看頁面。
更好的解決方案是將rel =「external」添加到鏈接,以告知JQM它是傳出鏈接。
<a href="myself.com?mobile=true" rel="external" >
而是因爲我無法運行一些代碼,我想以更改cookie,我必須通過查詢字符串參數,檢查它的preinit,然後將它在我的網頁看起來也餅乾在預先準備好並翻轉主人。
下面是我的完整解決方案,以防有人在那裏做同樣的事情。請注意,因爲我的網站使用別名,我不得不讀取Request.RawUrl並自己解析它,因爲Request.QueryString對象不包含我傳遞的值。
// reusable function that parses a string in standard query string format(foo=bar&dave=awesome) into a Dictionary collection of key/value pairs
// return the reference to the object, you have to assign it to a local un-instantiated name
// will accept a full url, or just a query string
protected Dictionary<string, string> parseQueryString(string url)
{
Dictionary<string, string> d = new Dictionary<string, string>();
if (!string.IsNullOrEmpty(url))
{
// if the string is still a full url vs just the query string
if (url.Contains("?"))
{
string[] urlArray = url.Split('?');
url = urlArray[1]; // snip the non query string business away
}
string[] paramArray = url.Split('&');
foreach (string param in paramArray)
{
if (param.Contains("="))
{
int index = param.IndexOf('=');
d.Add(param.Substring(0, index), param.Substring(++index));
}
}
}
return d;
}
然後我就用我的字典對象評估和重建我的網址與對面的移動價值,動態地設置肘杆的href。一些代碼明顯被遺漏了,但是爲了透視,base._iPage.QueryStringParams保存了我返回的字典對象,而base._iPage.IsMobile只是一個布爾屬性,我也通過我使用的頁面接口,我的所有頁面,和用戶控制等,可以交談。
// get the left side fo the url, without querystrings
StringBuilder url = new StringBuilder(Request.RawUrl.Split('?')[0]);
// build link to self, preserving query strings, except flipping mobile value
if (base._iPage.QueryStringParams.Count != 0)
{
if (base._iPage.QueryStringParams.ContainsKey("mobile"))
{
// set to opposite of current
base._iPage.QueryStringParams["mobile"] = (!base._iPage.IsMobile).ToString();
}
int count = 0;
url.Append('?');
// loop through query string params, and add them back on
foreach (KeyValuePair<string, string> item in base._iPage.QueryStringParams)
{
count++;
url.Append(item.Key + "=" + item.Value + (count == base._iPage.QueryStringParams.Count ? "" : "&"));
}
}
// assign rebuild url to href of toggle link
viewMobileButton.HRef = url.ToString();
}
然後在我的pageinit這是其中i實際檢查中,首先,quesry字符串,則該cookie時,如果這兩個時間都不存在,我運行我的移動檢測方法,和設置cookie,以及我的接口布爾財產易於訪問依賴它的條件。
QueryStringParams = base.parseQueryString(Request.RawUrl);
if (QueryStringParams.ContainsKey("mobile") ? QueryStringParams["mobile"].ToLower().Equals("true") : false)
{
Cookies.bakeCookie("isMobile", "true"); // create a cookie
IsMobile = true;
}
else if (QueryStringParams.ContainsKey("mobile") ? QueryStringParams["mobile"].ToLower().Equals("false") : false)
{
Cookies.bakeCookie("isMobile", "false"); // create a cookie
IsMobile = false;
}
else
{
IsMobile = base.mobileDetection();
}
if (IsMobile)
this.Page.MasterPageFile = "m-" + this.Page.MasterPageFile;
}