Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JavaScript img tag DOM question 1

Status
Not open for further replies.

Pleximus

Programmer
Jul 21, 2003
7
0
0
US
I am currently using the following code to call up images but the code works only in IE. Is there a way to code it so it works both in Netscape 6/7 and IE????

<img name=&quot;myImg&quot; src=&quot;javascript:document.myImg.src='assets/' + variable + '/imagename.gif'&quot;>

Plex
 
Maybe use Javascript.

Code:
<script>
var thePicA = new Image();
thePicA.src = &quot;assets/A/imagename.gif&quot;;

var thePicB = new Image();
thePicB.src = &quot;assets/B/imagename.gif&quot;;
 ...

var variable = &quot;B&quot;;
 ...
</script>

<img name=&quot;myImg&quot; src=&quot;javascript:document.myImg.src=eval('thePic' + variable + '.src')&quot;>
 
rac2,

thanks. I tried you eval method, but no luck. Netscape 6 and 7 don't seem to support any form of javascript shorthand within the src attribute of an img tag. I can't get ANYTHING written with Javascript successfully in the src attribute for NS 6/7.

Thanks tho

Plex
 
using DOM:

<img id=&quot;myImg&quot; src=&quot;&quot;/>

<script type=&quot;text/javascript&quot;>
document.getElementById(&quot;myImg&quot;).src = 'assets/' + variable + '/imagename.gif';
</script>


=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
I may be wrong but O think you're going about it the wrong way...

right now, you have an HTML IMG tag as follows:
<img name=&quot;myImg&quot; src=&quot;javascript:document.myImg.src='assets/' + variable + '/imagename.gif'&quot;>

I don't think you can use variables in a straight HTML document. In order to make this work, you'd have to write your page in some other scripting language like PERL, PHP, etc. Then, I think you'd have more luck by declaring the SRC value as a variable and calling the IMG tag like this:
<img name=&quot;myImg&quot; src=variable> where variable is &quot;assets/<something>/imagename.gif&quot;.

As another thought, you might also try something like this:
<img name=&quot;myImg&quot; src=&quot;javascript:imgSource()&quot;> where the SRC value is a javascript function and the function where the fnction determines the SRC value, or the SRC value is passed to the function: <img name=&quot;myImg&quot; src=&quot;javascript:imgSource(A)&quot;>.

I haven't tried either of these but I think this will put you on the right track.

There's always a better way. The fun is trying to find it!
 
jemminger,

thanks. You are most studly! This is the best solution (out of about a 100 variations) that I have tried.

Thanks!

Plex
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top