Data Browser - Viewing Site  Sector 23 Code Bank Logged in as:  Guest  




           


Dynamically Add jQuery Mobile Support to a .NET Page
The following will dynamically add jQuery Mobile support to a page if the device is a mobile device.
This allows non-mobile devices to not download the jQuery mobile framework. jQuery mobile tags will be ignored if it is non-mobile.

If you have already included jQuery on your page, you can delete that line.

This assumes you have copied the jQuery and jQuery mobile files to your .NET project. Change the paths as needed.

Usage: Place these in a static class somewhere, and in your master page/base page, add:
this.Page.CheckIncludeJqueryMobile();

> > > > > > > >

const string JQUERY_PATH = "~/Scripts/jquery-1.9.1.js";
const string JQUERY_MOBILE_PATH = "~/Scripts/JQM/jquery.mobile-1.4.4.min.js";
const string JQUERY_MOBILE_CSS_PATH = "~/Scripts/JQM/jquery.mobile-1.4.4.min.css";

///
/// Include jQuery Mobile on a page if user has a mobile device
///

///
public static void CheckIncludeJqueryMobile(this Page page)
{
if (page.Request.Browser.IsMobileDevice)
{
IncludeJquery(page); // reqd for jqm

RegisterClientScriptInclude(page, "jqm-js", JQUERY_MOBILE_PATH);

HtmlLink myHtmlLink = new HtmlLink();
myHtmlLink.Href = JQUERY_MOBILE_CSS_PATH;
myHtmlLink.Attributes.Add("rel", "stylesheet");
myHtmlLink.Attributes.Add("type", "text/css");
page.Header.Controls.Add(myHtmlLink);
}
}

///
/// Include jQuery on a page
///

///
public static void IncludeJquery(Page page)
{
RegisterClientScriptInclude(page, "jquery", JQUERY_PATH);
}

public static void RegisterClientScriptInclude(Page page, string key, string url)
{
if (url.StartsWith("~"))
url = page.ResolveUrl(url);
ScriptManager.RegisterClientScriptInclude(page, page.GetType(), key, url);
}

Created By: amos 9/30/2014 4:19:20 PM
Updated: 9/30/2014 4:23:49 PM