Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

html - Javascript function return src path

I'm trying to set an 'src' by calling a javascript function that returns the path like so:

<img src="getImagePath()" />

the function:

function getImagePath(){

     return "images/image1.png";

}

But it doesn't seem to work. Anything I'm missing? Thanks to anyone pointing me in the right direction.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The src attribute takes an URL, not JavaScript. You might want to try

<img src="pixel.gif" onload="this.onload=null; this.src=getImagePath();" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...