PDA

View Full Version : Javascript Browser Detection



Hypertext
07-09-2008, 05:35 PM
To add a css file for opera and safari, I'm using this code I found:


function isSaf() {
return = (document.childNodes && !document.all && !navigator.taintEnabled && !navigator.accentColorName) ? true : false;
}

function isOp() {
return (window.opera) ? true : false;
}

function loadPatches(dir) {
if (document.getElementsByTagName() && document.createElement()) {
var head = document.getElementsByTagName("head")[0];
if (head) {
var css = new Array();
if (isSaf()) {
css.push("saf.css");
} else if (isOp()) {
css.push("opera.css");
}

if (css.length) {
var link = document.createElement("link");
link.setAttribute("rel", "stylesheet");
link.setAttribute("type", "text/css");
link.setAttribute("media", "screen, projection");
for (var i = 0; i < css.length; i++) {
var tag = link.cloneNode(true);
tag.setAttribute("href", dir + css[0]);
head.appendChild(tag);
}
}
}
}
}
$(document).ready(function() {
loadPatches('css/')
});
With a bit of editing I got it working, but for some strange reason, It's not liking the opera detection, safari detection works, but opera doesn't, I've used firebug to detect whether it has been written to the document and it hasn't. Any ideas?

Calon
07-09-2008, 05:47 PM
Opera is a fairly new browser, your code must be ancient.

Hypertext
07-09-2008, 05:59 PM
The validation is very un-error-prone, it's just "window.opera"...

Want to hide these adverts? Register an account for free!