Presently, I am attempting to devise CSS Selectors using JavaScript specifically tailored for particular web browsers and mobile devices. The current method in place is as follows:
// IDENTIFY BROWSER AND APPLY CORRESPONDING CSS METHOD
function getBrowserName() {
var name = "Unknown";
if(navigator.userAgent.indexOf("MSIE")!=-1){
name = "msie";
} if(navigator.userAgent.indexOf("Trident")!=-1){
name = "msie";
} if(navigator.userAgent.indexOf("Edge")!=-1){
name = "msie";
} else if(navigator.userAgent.indexOf("Firefox")!=-1){
name = "firefox";
} else if(navigator.userAgent.indexOf(" OPR/")>=0){
name = "opera";
} else if(navigator.userAgent.indexOf("Chrome") != -1){
name = "chrome";
} else if(navigator.userAgent.indexOf("Safari")!=-1){
name = "safari";
}
return name;
}
if (getBrowserName() != "Unknown"){
document.getElementsByTagName('html')[0].className += "is-" + getBrowserName(name);
}
The above method can be called by
.is-(browser-name element-selector { css }
. However, the same implementation does not seem to function effectively for mobile devices. Here's a glimpse at my code...
var mobile = (/iphone|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()));
if (mobile) {
document.getElementsByTagName('html')[0].className += "is-mobile";
}
In other instances, I have been utilizing jQuery for tasks outside of the if (mobile)
segment which has proven successful. Nonetheless, I am keen on streamlining this process using standard CSS and JavaScript. Although a functional code snippet cannot be provided since compatibility is contingent upon specific browser usage, an illustrative sample has been included below.
Is there anyone able to identify why the code functions appropriately for various browsers but encounters obstacles when targeting mobile devices? Appreciate any assistance!
// FUNCTIONAL
function getBrowserName() {
var name = "Unknown";
if (navigator.userAgent.indexOf("MSIE") != -1) {
name = "msie";
}
if (navigator.userAgent.indexOf("Trident") != -1) {
name = "msie";
}
if (navigator.userAgent.indexOf("Edge") != -1) {
name = "msie";
} else if (navigator.userAgent.indexOf("Firefox") != -1) {
name = "firefox";
} else if (navigator.userAgent.indexOf(" OPR/") >= 0) {
name = "opera";
} else if (navigator.userAgent.indexOf("Chrome") != -1) {
name = "chrome";
} else if (navigator.userAgent.indexOf("Safari") != -1) {
name = "safari";
}
return name;
}
if (getBrowserName() != "Unknown") {
document.getElementsByTagName('html')[0].className += "is-" + getBrowserName(name);
}
// ISSUE WITH MOBILE
var mobile = (/iphone|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()));
if (mobile) {
document.getElementsByTagName('html')[0].className += "is-mobile";
}
// FUNCTIONAL
if (mobile) {
$(".about-text").css("width", "90%");
$("body").css("font-size", "50px");
$("section").css("padding-top", "160px");
}
/* FUNCTIONS */
.is-msie hr {
margin: 50px 0 10px 0;
}
/* ISSUE */
.is-mobile hr {
margin: 50px 0 10px 0;
}
<!--REPEATING TABLE WITH HR FOR SEPARATORS -->
....(content removed for brevity)
</div>