Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #26621

Re: quick question: how to dynamically an image source file?

From Joao Rodrigues <groups_jr-1@yahoo.com.br>
Newsgroups comp.lang.javascript
Subject Re: quick question: how to dynamically an image source file?
Date 2015-01-19 10:48 -0200
Organization Aioe.org NNTP Server
Message-ID <m9iue0$bah$1@speranza.aioe.org> (permalink)
References <9607d1f5-10c0-4703-901c-bab1932cb04a@googlegroups.com> <XnsA427734C3E58Ceejj99@194.109.133.133>

Show all headers | View raw


On 19/01/2015 08:20, Evertjan. wrote:
> justaguy wrote:

>> I have the following page/code to set default image src of "exp.png",
>> then if var b value is no longer 0 use srk.png as the image src. 
>>
>> The html code is like this:
>> <img src="" id="myImg">
>>
>>
>> js code:
>> var b = 0;
>>      
>> var Pic = new Image(22,22);
>> if (b == 0) {


Unless you guarantee that the b variable will always receive a number, I
suggest using the strict equals operator if (b === 0) in order to avoid
type coercion.

>>      Pic.src = "exp.png";
>>      document['myImg'].src = Pic.src;
> 
> document['myImg'] ??????
> 
> Perhaps that worked on IE minus 2.7,
> in this century we do:
> 
> document.getElementById('myImg')
> 

document.getElementById is preferable, but the OP could safely use the
document.images HTMLCollection in this case:

document.images.namedItem("myImg").src

or

document.images.[0].src // if the OP knows the index in advance.

> 
>>     }
>> else {
>>      Pic.src = "srk.png";     
>>      document['myImg'].src = Pic.src;
>>     }
>>
>> Result:
>> The image is not rendered.  What's wrong?
> 

Check if the src path is correct and use the onerror event handler, e.g

Pic.onerror = function () {
  alert('error loading ' + this.src);
};

I'd suggest using a web developer tool to debug (press F12). See
<http://devtoolsecrets.com/>

-- 
Joao Rodrigues

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

quick question: how to dynamically an image source file? justaguy <lichunshen84@gmail.com> - 2015-01-18 19:39 -0800
  Re: quick question: how to dynamically an image source file? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-19 11:20 +0100
    Re: quick question: how to dynamically an image source file? Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-01-19 10:48 -0200
      Re: quick question: how to dynamically an image source file? justaguy <lichunshen84@gmail.com> - 2015-01-19 07:52 -0800
        Re: quick question: how to dynamically an image source file? JJ <jj4public@vfemail.net> - 2015-01-20 02:47 +0700
          Re: quick question: how to dynamically an image source file? justaguy <lichunshen84@gmail.com> - 2015-01-19 14:24 -0800
            Re: quick question: how to dynamically an image source file? JJ <jj4public@vfemail.net> - 2015-01-20 07:31 +0700
              Re: quick question: how to dynamically an image source file? justaguy <lichunshen84@gmail.com> - 2015-01-20 02:26 -0800
            Re: quick question: how to dynamically an image source file? Tim Streater <timstreater@greenbee.net> - 2015-01-20 12:08 +0000
              Re: quick question: how to dynamically an image source file? justaguy <lichunshen84@gmail.com> - 2015-01-20 12:20 -0800
          Re: quick question: how to dynamically an image source file? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-20 14:06 +0100
            Re: quick question: how to dynamically an image source file? Tim Streater <timstreater@greenbee.net> - 2015-01-20 14:52 +0000
              Re: quick question: how to dynamically an image source file? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-20 20:47 +0100
        Re: quick question: how to dynamically an image source file? Denis McMahon <denismfmcmahon@gmail.com> - 2015-01-19 21:15 +0000
          Re: quick question: how to dynamically an image source file? justaguy <lichunshen84@gmail.com> - 2015-01-19 14:25 -0800

csiph-web