Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Script used to redirect mobile browsers to Blogger Mobile Template view
<script type='text/javascript'>
//<![CDATA[ Wrap in CDATA to prevent issues with < and & characters in Blogger xml template
//Function to replace or add a parameter to current URL. From:
function insertParam(key, value) {
key = escape(key); value = escape(value);
var kvp ='&');
var i = kvp.length; var x; while (i--) {
x = kvp[i].split('=');
if (x[0] == key) {
x[1] = value;
kvp[i] = x.join('=');
if (i < 0) { kvp[kvp.length] = [key, value].join('='); }
//this will reload the page, it's likely better to store this until finished = kvp.join('&');
var agent = navigator.userAgent;
var isIEMobile = !!agent.match(/IEMobile/i),
isOperaMobile = !!agent.match(/Opera Mobi/i),
isSafariMobile = !!agent.match(/Mobile Safari/i),
isOtherMobile = !!agent.match(/Mobile/i);
//These should already work:
//isIphone = !!agent.match(/iPhone/i),
//isBlackberry = !!agent.match(/blackberry/i),
//Check if mobile template parameter already exists in URL
var isMobileTemplate = !!location.toString().match(/[?&]m=[01]/i);
if (!isMobileTemplate) { //Don't run if parameter already exists
if (isIEMobile | isOperaMobile | isSafariMobile | isOtherMobile) {
//alert("Redirect to mobile template view");
insertParam("m", 1);
} else {
//alert("Don't redirect useragent:" + agent);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment