Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code to utilize different sub-templates for desktop and mobile in Joomla. Usage: Add right after the opening <html> tag
// Desktop to Mobile redirect and vise-versa
// Usage: Add right after the opening <html> tag
// For the desktop template:
<!-- Browser detect and redirect -->
<script type="text/javascript">
(function() {
// Get URL query strings
function getQS(v) {
var qs = window.location.search.substring(1);
var qsArray = qs.split("&");
for (i = 0; i < qsArray.length; i++) {
var qp = qsArray[i].split("=");
if (qp[0] == v) {
return true;
}
}
}
if (getQS('force')) {
document.getElementsByTagName('a').onclick = function(el) {
if (el.href.indexOf('?') > 0) {
el.href = el.href + '&force=1';
} else {
el.href = el.href + '?force=1';
}
}
}
if (getQS('m') === undefined) {
if (getQS('force')) return;
var curUrl = window.location.href;
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
var getLastChar = curUrl.substr((curUrl.length) - 1);
if (((/Android/i).test(userAgent) && (/Mobile/i).test(userAgent)) || (/BlackBerry|iPhone|iPod|Opera Mini|IEMobile/i).test(userAgent)) {
if (curUrl.indexOf('?') > 0) {
window.location.replace(curUrl + '&m=1');
} else {
window.location.replace(curUrl + '?m=1');
}
}
}
})();
</script>
// For the mobile template:
<!-- Browser detect and redirect -->
<script type="text/javascript">
(function() {
// Get URL query strings
function getQS(v) {
var qs = window.location.search.substring(1);
var qsArray = qs.split("&");
for (i = 0; i < qsArray.length; i++) {
var qp = qsArray[i].split("=");
if (qp[0] == v) {
return true;
}
}
}
if (getQS('m')) {
if (getQS('mobile')) return;
var curUrl = window.location.href;
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
var getLastChar = curUrl.substr((curUrl.length) - 1);
var newUrl = false;
if (!(((/Android/i).test(userAgent) && (/Mobile/i).test(userAgent)) || (/BlackBerry|iPhone|iPod|Opera Mini|IEMobile/i).test(userAgent))) {
newUrl = curUrl.split('?');
newUrl = newUrl[0];
if(newUrl) {
window.location.replace(newUrl);
}
}
}
})();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.