|
Hi all. I'm pretty new to javascript and can't figure this one out. I am looking for a way on page load to detect the resolution of the bowser, and then display a different image based on the results. For instance, if resolution is <= 800 display image 1, else if resolution >= 801 display image 2. Can anyone help me with something like that?
|
|
|
It's very easily!
Code Here
<script>
function makeImage()
{
var ScrW=screen.width;
var ScrH=screen.height;
var img=(ScrW>800)?'/forum/img1.jpg':'/forum/img2.jpg';
document.write('<img src="' + img + '">');
}
</script>
recode yourself for free
|
|
|
|
|
Hmmm, I tried out your code but it doesn't seem to do anything.
|
|
|
Ek, i tested on IE and FF, it works.
You can chat with me on Yahoo Messenger ad o0DarkEvil0o for further help.
|
|
|
|
|
Hi, you must call makeImage() function where you want to display the image. Go my page to get how it works on both IE and FF. Example
|
|
|
AHHH! Got it, excellent. Thank you very much.
|
|
|
|
|
I am having problems with it again. I get a sytax error after I altered the code for 3 possible images. Do you see what is wrong in this section?
<script>
var ScrW=screen.width;
var ScrH=screen.height;
function getImage()
{
var img= if (ScrW<1023)
'/forum/bimg800.jpg';
else if (ScrW>1023&&ScrW<1151)
'/forum/bimg1024.jpg';
else if (ScrW>1151)
'/forum/bimg1280.jpg';
else
'/forum/bimg1024.jpg';
return img;
}
</script>
|
|
|
|
|
I never code that isn't explicit.
Edited your code here, it is in syntax: variable = (condition)?'<value when codition==true>':'<value when codition==false>';
<script>
var ScrW=screen.width;
var ScrH=screen.height;
function getImage()
{
var img=(ScrW<1023)?('/forum/bimg800.jpg'):((ScrW<1151)?('/forum/bimg1024.jpg'):((ScrW>1151)?('/forum/big1280.jpg'):('/forum/big1024.jpg')));
return img;
}
</script>
|
|
|
|
|