1/*! 2* JS Redirection Mobile 3* 4* Developed by 5* Sebastiano Armeli-Battana (@sebarmeli) - http://www.sebastianoarmelibattana.com 6* Dual licensed under the MIT or GPL Version 3 licenses. 7*/ 8/* 9Copyright (c) 2011 Sebastiano Armeli-Battana (http://www.sebastianoarmelibattana.com) 10 11This program is free software: you can redistribute it and/or modify 12it under the terms of the GNU General Public License as published by 13the Free Software Foundation, either version 3 of the License, or 14(at your option) any later version. 15 16This program is distributed in the hope that it will be useful, 17but WITHOUT ANY WARRANTY; without even the implied warranty of 18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19GNU General Public License for more details. 20 21You should have received a copy of the GNU General Public License 22along with this program. If not, see <http://www.gnu.org/licenses/>. 23*/ 24 25/* 26* This script will cover a basic scenario of full JS mobile redirection. 27* The user will be redirected to the mobile version of the site (home page) 28* if it's trying to access the site from a mobile device. This check is 29* mainly done checking the User-Agent string. 30* The mobile URL will be obtained appending a prefix (default is "m") to 31* the hostname of the current URL. It's used a naked domain to avoid conflict with 32* www. 33* 34* In some cases the user needs to be redirected to the Desktop version of the site 35* from a mobile device. This happens when the user clicks on a link such as "Go to full site" 36* or when there is a specific parameter in the querystring. 37* 38* In that case a new key/value in sessionStorage (for modern browsers) 39* will be set and until the user doesn't close browser window or tab it will access 40* to the desktop version from a mobile device. There is a fallback for old browsers that 41* don't support sessionStorage, and it will use a cookie. The cookie will expiry in one hour 42* (default value) or you configure the expiry time. 43* 44* To use this function, you need to call it as SA.redirection_mobile(config); 45* E.g. SA.redirection_mobile ({noredirection_param : "noredirection", mobile_prefix : "mobile", cookie_hours : "2" }) 46* or 47* E.g. SA.redirection_mobile ({mobile_url : "mobile.whatever.com/example", mobile_sheme : "https" }) 48* or 49* E.g. SA.redirection_mobile ({mobile_prefix : "mobile"}) 50* or 51* E.g. SA.redirection_mobile ({mobile_prefix : "mobile", mobile_scheme : "https", redirection_paramName : "modile_redirect"}) 52* or 53* E.g. SA.redirection_mobile ({mobile_url : "mobile.whatever.com/example", tablet_redirection : "true"}) 54* or 55* E.g. SA.redirection_mobile ({{mobile_url : "mobile.whatever.com/example", beforeredirection_callback : (function(){alert('2')}) }}) 56* or 57* E.g. SA.redirection_mobile ({{mobile_url : "mobile.whatever.com", tablet_url : "tablet.whatever.com") 58* 59* 60* @link http://github.com/sebarmeli/JS-Redirection-Mobile-Site/ 61* @author Sebastiano Armeli-Battana 62* @version 0.9.5 63* @date 25/07/2011 64* 65*/ 66 67/*globals window,document, navigator, SA */ 68if (!window.SA) {window.SA = {};} 69 70/* 71* @param configuration object containing three properties: 72* - mobile_prefix : prefix appended to the hostname (such as "m" to redirect to "m.domain.com") 73* - mobile_url : mobile url to use for the redirection (such as "mobile.whatever.com" to redirect to "whatever.com" ) 74* - mobile_scheme : url scheme (http/https) of the mobile site domain 75* - cookie_hours : number of hours the cookie needs to exist after redirection to desktop site 76* - noredirection_param : parameter to pass in the querystring of the URL to avoid the redirection (the value must be equal to "true"). 77* It's also the name of the item in the localStorage (or cookie name) to avoid mobile 78* redirection. Default value is "noredirection". Eg: http://domain.com?noredirection=true 79* - tablet_redirection : boolean value that enables/disables(default) the redirection for tablet such as iPad, Samsung Galaxy Tab, Kindle or Motorola Xoom. - Default:false 80* Default Url for redirection will be the same as the one for mobile devices. 81* - tablet_url : url to use for the redirection in case user is using a tablet to access the site 82* - keep_path : boolean to determine if the destination url needs to keep the path from the original url 83* - keep_query : boolean to determine if the destination url needs to keep the querystring from the original url 84* - beforeredirection_callback : callback launched before the redirection happens 85*/ 86SA.redirection_mobile = function(configuration) { 87 88// Helper function for adding time to the current date 89var addTimeToDate = function(msec) { 90 91// Get the current date 92var exdate = new Date(); 93 94// Add time to the date 95exdate.setTime(exdate.getTime() + msec); 96 97//Return the new Date 98return exdate; 99 100}; 101 102// Helper function for getting a value from a parameter in the querystring 103var getQueryValue = function(param) { 104 105if (!param) { 106return; 107} 108 109var querystring = document.location.search, 110queryStringArray = querystring && querystring.substring(1).split("&"), 111i = 0, 112length = queryStringArray.length; 113 114for (; i < length; i++) { 115var token = queryStringArray[i], 116firstPart = token && token.substring(0, token.indexOf("=")); 117if (firstPart === param ) { 118return token.substring(token.indexOf("=") + 1, token.length); 119} 120} 121 122}; 123 124// Retrieve the User Agent of the browser 125var agent = navigator.userAgent.toLowerCase(), 126 127// Constants 128FALSE = "false", 129TRUE = "true", 130 131// configuration object 132config = configuration || {}, 133 134// parameter to pass in the URL to avoid the redirection 135redirection_param = config.noredirection_param || "noredirection", 136 137// prefix appended to the hostname 138mobile_prefix = config.mobile_prefix || "m", 139 140// new url for the mobile site domain 141mobile_url = config.mobile_url, 142 143// protocol for the mobile site domain 144mobile_protocol = config.mobile_scheme ? 145config.mobile_scheme + ":" : 146document.location.protocol, 147 148// URL host of incoming request 149host = document.location.host, 150 151// value for the parameter passed in the URL to avoid the redirection 152queryValue = getQueryValue(redirection_param), 153 154// Compose the mobile hostname considering "mobile_url" or "mobile_prefix" + hostname 155mobile_host = mobile_url || 156(mobile_prefix + "." + 157(!!host.match(/^www\./i) ? 158host.substring(4) : 159host)), 160 161// Expiry hours for cookie 162cookie_hours = config.cookie_hours || 1, 163 164// Parameters to determine if the pathname and the querystring need to be kept 165keep_path = config.keep_path || false, 166keep_query = config.keep_query || false, 167 168// new url for the tablet site domain 169tablet_host = config.tablet_url || mobile_host, 170 171// Check if the UA is a mobile one (iphone, ipod, android, blackberry) 172isUAMobile =!!(agent.match(/(iPhone|iPod|blackberry|android 0.5|htc|lg|midp|mmp|mobile|nokia|opera mini|palm|pocket|psp|sgh|smartphone|symbian|treo mini|Playstation Portable|SonyEricsson|Samsung|MobileExplorer|PalmSource|Benq|Windows Phone|Windows Mobile|IEMobile|Windows CE|Nintendo Wii)/i)); 173 174 175// Check if the referrer was a mobile page (probably the user clicked "Go to full site") or in the 176// querystring there is a parameter to avoid the redirection such as "?mobile_redirect=false" 177// (in that case we need to set a variable in the sessionStorage or in the cookie) 178if (document.referrer.indexOf(mobile_host) >= 0 || queryValue === TRUE ) { 179 180if (window.sessionStorage) { 181window.sessionStorage.setItem(redirection_param, TRUE); 182} else { 183document.cookie = redirection_param + "=" + TRUE + ";expires="+ 184addTimeToDate(3600*1000*cookie_hours).toUTCString(); 185} 186} 187 188// Check if the sessionStorage contain the parameter 189var isSessionStorage = (window.sessionStorage) ? 190(window.sessionStorage.getItem(redirection_param) === TRUE) : 191false, 192 193// Check if the Cookie has been set up 194isCookieSet = document.cookie ? 195(document.cookie.indexOf(redirection_param) >= 0) : 196false; 197 198// Check if the device is a Tablet such as iPad, Samsung Tab, Motorola Xoom or Amazon Kindle 199if (!!(agent.match(/(iPad|SCH-I800|xoom|kindle)/i))) { 200 201// Check if the redirection needs to happen for iPad 202var isUATablet = (config.tablet_redirection === TRUE || !!config.tablet_url) ? true : false; 203isUAMobile = false; 204} 205 206// Check that User Agent is mobile, cookie is not set or value in the sessionStorage not present
207if ((isUATablet || isUAMobile) && !(isCookieSet || isSessionStorage)) { 208 209// Callback call 210if (config.beforeredirection_callback) { 211if (!config.beforeredirection_callback.call(this)) { 212return; 213} 214} 215 216var path_query = ""; 217 218if(keep_path) { 219path_query += document.location.pathname; 220} 221 222if (keep_query) { 223path_query += document.location.search; 224} 225 226if (isUATablet){ 227document.location.href = mobile_protocol + "//" + tablet_host + path_query; 228} else if (isUAMobile) { 229document.location.href = mobile_protocol + "//" + mobile_host + path_query; 230} 231 232} 233};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.