codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  different image displayed based on screen resolution  socozima at 17:57 on Tuesday, May 20, 2008
 

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?

  Re: different image displayed based on screen resolution  o0DarkEvil0o at 16:04 on Wednesday, May 21, 2008
 

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


  Re: different image displayed based on screen resolution  socozima at 16:36 on Wednesday, May 21, 2008
 

Hmmm, I tried out your code but it doesn't seem to do anything.

  Re: different image displayed based on screen resolution  o0DarkEvil0o at 16:53 on Wednesday, May 21, 2008
 

Ek, i tested on IE and FF, it works.
You can chat with me on Yahoo Messenger ad o0DarkEvil0o for further help.

  Re: different image displayed based on screen resolution  socozima at 17:06 on Wednesday, May 21, 2008
 

I don't have yahoo messenger. maybe you can look at this and see what i did wrong. ing1.jpg and img2.jpg are in the same directory.

http://www.gis.net/~jwgrundy/test/new_page_2.htm

  Re: different image displayed based on screen resolution  o0DarkEvil0o at 17:30 on Wednesday, May 21, 2008
 

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

  Re: different image displayed based on screen resolution  socozima at 17:40 on Wednesday, May 21, 2008
 

AHHH! Got it, excellent. Thank you very much.

  Re: different image displayed based on screen resolution  o0DarkEvil0o at 17:45 on Wednesday, May 21, 2008
 

Lol, your're welcome!

  Re: different image displayed based on screen resolution  socozima at 00:58 on Wednesday, May 28, 2008
 

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>



  Re: different image displayed based on screen resolution  o0DarkEvil0o at 03:51 on Wednesday, May 28, 2008
 

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>









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  Re: different image displayed based on screen resolution
•  new here, can anyone help me with a problem involving pointer?
•  Ask for suggestions on blog template :)
•  Re: Get the image name
•  A new simple way to make a image slider- Javascript Code
•  Re: convert minutes into hours and minutes
•  how to create forum on my own in java is it possible
•  Re: mkdirs
•  Help with recursion { recursive helper function }


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2008