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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting the src = to a function 1

Status
Not open for further replies.

aTekTipsUser

Programmer
Nov 8, 2004
68
US
Is it possible to set the src (in the img tag) = to a function or even a variable.

Instead of setting <img src = "\imgLocation.jpg"> is it possible to set it = to a function or variable.

I tried setting up a javascript function getPath, and in the img tag setting the src = "getPath();".

I want to set up a global folder path, then concatenate the file name. This way, it would ease the task of changing folder locations. Thanks!

 
I don't think that will work, but you CAN use a javascript function to set the src for an image. You just have to call the function from the body onload event or something similar:
Code:
function getPath() {
  // do what you need to get the path here
  document.getElementById("imgTag").src = pathVar;
}
 .
 .
 .
<body onload="getPath();">
 .
 .
 .
<img id="imgTag" src="">


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Do you want to set it dynamically, or only at run time? If it's set at run time (which your question implies) try this:
Code:
<script language="javascript">
document.write('<img src = "' + getPath() + '">'
</script>

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
BTW, you would embed my code right where your old <img> tag was

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Thanks for the reply!

I tried embedding the code <script language=javascript>document.write('<img src = "' + getPath() + '">')</script> where my old <img> tag was, but it does not recognize. I tried hard coding the src path and it still doesn't recognize <script language=javascript>document.write('<img src = "test.jpg">')</script>. When I take out the document.write it works. document.write('Test') will write correct.
 
Your code was good. Thanks! I was referencing a server with the syntax \\servername\path.jpg and it worked when I entered it as <img src=\\servername\path.jpg> but when I used the document.write, it didn't work. I fixed it by putting file://serverName/path.jpg.

Your help is much appreciated. It will save much time!
 
Would it be possible to pull out of the Web.config ConfigurationSettings in javascript? In my asp.net code I just refer to it ConfigurationSettings.AppSettings("Path").
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top