Click to See Complete Forum and Search --> : JavaScript,resolution and CSS


ashur112
01-12-2010, 10:52 AM
Hi.
I have a site that uses a big background. I found a script that looks up the users resolution and sets the bg based on that (mainly a question of setting a proper image for those 1024x768 users).The bg is fetched from a style sheet xxx.css.The script works by "onload".

--------------------------------------------------
...
<script type="text/javascript">
<!--
function switchCSS() {
if (window.screen.availWidth < 1280) {
var myLink = document.getElementsByTagName('link')[0];
if (myLink) myLink.setAttribute('href', 'xxx1024.css', 0);
}
}
//-->
</script>

</head>
<body onload="switchCSS();">
...
------------------------------------------------

How do I modify the script so that it looks up not only the resolution, but also the browser in question (IE) and gets the style sheet by gathered info (either xxx1024.css or xxx1024IE.css.)?
+I would like to make it a separate .js file, so that it doesn't clutter up the code.
I would be great if somebody could help.My own scripting skills are limited.:(

simonstaton
01-12-2010, 11:44 AM
I am not entirely sure what you are looking for but the easiest way to change the background image according to browser or you can change it to be according to resolution would be:

onload(){
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById('body').style.backgroundImage = "url(bg.png)";
}
else {
if (document.layers) { // Netscape 4
document.body.backgroundImage = "url(bg.png)";
}
else { // IE 4
document.all.body.style.backgroundImage = "url(bg.png)";
}
}

Kor
01-12-2010, 01:15 PM
To detect IE I would have rather used the IE conditional statements (http://msdn.microsoft.com/en-us/library/ms537512%28VS.85%29.aspx):

rnd me
01-12-2010, 03:14 PM
To detect IE I would have rather used the IE conditional statements (http://msdn.microsoft.com/en-us/library/ms537512%28VS.85%29.aspx):

i recommend avoiding conditional comments because they break when you compress/minify/pack your code.

i use this for detecting IE: it's standard js, and compresses just fine:

IE=("\v"=="v");