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


Groups > comp.lang.javascript > #26623

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

Newsgroups comp.lang.javascript
Date 2015-01-19 07:52 -0800
References <9607d1f5-10c0-4703-901c-bab1932cb04a@googlegroups.com> <XnsA427734C3E58Ceejj99@194.109.133.133> <m9iue0$bah$1@speranza.aioe.org>
Message-ID <6abe327d-c487-4e03-8a6f-f7f65ebc8bb0@googlegroups.com> (permalink)
Subject Re: quick question: how to dynamically an image source file?
From justaguy <lichunshen84@gmail.com>

Show all headers | View raw


On Monday, January 19, 2015 at 7:48:11 AM UTC-5, J.R. wrote:
> 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

Thanks.

Now, I'm having trouble to load the js function.

Please see code below.

function f1() {
// in case var b is not available from parent page
if (!b) {b = 0;}
alert(b);

// dynamim image code below
...

// just run the function
 f1();

}


html code:
to load the js
<div id="myD">
...
</div>


outcome:
The alert failed to alert, which indicates that the function is not run.  What could be wrong?

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