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)?'img1.jpg':'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)
'bimg800.jpg';
else if (ScrW>1023&&ScrW<1151)
'bimg1024.jpg';
else if (ScrW>1151)
'bimg1280.jpg';
else
'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)?('bimg800.jpg'):((ScrW<1151)?('bimg1024.jpg'):((ScrW>1151)?('big1280.jpg'):('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
• I Need Help
• User input validation
• Create a wrapper for a COM Dll
• HashMaps
• HashMaps
• NEED HELP! Urgent
• Re: Exciting Open Source Project in C++
• Re: The Ballarat Airline Company (BAC)
• Re: problem getting prompt window to work on IE


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