Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > reset the width of image

Reply
Thread Tools

reset the width of image

 
 
wakun@wakun.com
Guest
Posts: n/a
 
      03-06-2006
Hi there,
First of all, I am trying to reset the width of the image while
loading

<img src='s.jpg' onload="javascript:if(this.width>120)
this.width=100;">

It's ok. And then I am going to make the code more flexible

<img src='s.jpg'
onload="javascript:if(this.width>javascript:window .document.body.offsetWidth)
this.width=50;">

this time, I compare the image width with the offsetWidth, however, I
found the code above didn't work no matter how small offsetWidh is
made.

BTW, I would like to make the width of image fit the current width of
the document, only HTML works fine under IE

<img src='s.jpg' width="100%" LEFTMARGIN=10>

But when applying the above code to the image of small size, it always
try to strech the image ,which will then look very bad.

I am trying to apply the code on condiiton: when the width of image is
smaller than a given value, do nothing; otherwise add "width=100%
LEFTMARGIN=10". Here is the javascript I tried

<img src='s.jpg' onload="javascript:if(this.width>100)
javascript:document.write('WIDTH=100% LEFTMARGIN=10');">

However, it didn't give the result I want. Would someone please give me
some clues?

thanks in advance.

 
Reply With Quote
 
 
 
 
web.dev
Guest
Posts: n/a
 
      03-06-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> <img src='s.jpg' onload="javascript:if(this.width>100)
> javascript:document.write('WIDTH=100% LEFTMARGIN=10');">
>
> However, it didn't give the result I want. Would someone please give me
> some clues?


1. Get rid of javascript pseudo-protocols. If you do a search about it
within this group, you'll get lots of reasons why not to use it.
2. You're using document.write. It's performing an action which you
are not expecting.

I prefer not to use inline javascript, but here's something that can
get you started:

javascript:

function adjustImg(oImg)
{
if(oImg.width > 100)
{
oImg.style.width = "100%";
oImg.style.marginLeft = "10px";
}
}

html:

<img src = "path/name.ext" onload = "adjustImg(this)">

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help! Fixed width table with a 100% width image in it is assuming theimages intrinsic size, although it does shrink the image satya.komatineni@gmail.com HTML 1 12-29-2007 08:32 PM
You will not be able to reset the access - can't reset. jc MCAD 0 12-09-2007 08:58 PM
CISCO 7XX / 761 password reset or reset to factory defaults Philipp Flesch Cisco 3 06-18-2006 07:47 PM
Textbox width scaling to width of data not width of page? AndrewF ASP .Net 1 10-10-2005 04:38 PM
Re: resized image in table extends table width to original image width Sean Jorden HTML 1 08-19-2003 08:59 AM



Advertisments