Forum Moderators: open

Message Too Old, No Replies

Changing image path within javascript

Image Path

         

slick

4:20 pm on Jul 30, 2008 (gmt 0)

10+ Year Member



Hello All,

I would like to ask a very basic question relating to the image path.

I tried accessing this path <img src=\"icon-alert.gif\"/> but it needs the whole path which is:

Images/Icons/Icon-alert.gif

I simple tried putting it in there but it wouldn't work

Any help will be highly appreciated. Thanks.

Fotiman

5:27 pm on Jul 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm not sure how you're using this. You've got your quotes escaped, but not the end slash... so I'm not exactly sure how this is being used. Can you provide your actual code example?

Short answer:
<img src="Images/Icons/Icon-alert.gif">
That's assuming there is an Images folder in the same directory as the HTML file (or the <base>).

slick

5:33 pm on Jul 30, 2008 (gmt 0)

10+ Year Member



function GetNewIcon(item)
{
if (item.getMember('Viewed').get_value()!=1)
return "<img src=\"images/icons/icon-alert.gif\"/>";
}

slick

5:33 pm on Jul 30, 2008 (gmt 0)

10+ Year Member



Though it's not working.. I tried using different variations.. such as forward slashes instead of back slashes.. etc

slick

5:37 pm on Jul 30, 2008 (gmt 0)

10+ Year Member



This worked:
function GetNewIcon(item)
{
if (item.getMember('Viewed').get_value()!=1)
return "<img src=\"images/icons/icon-alert.gif\"/>";
}

Thank you for the quick reply though.

Regards,
S

Fotiman

7:57 pm on Jul 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That looks identical to what you posted above where you said it didn't work, so I'm not sure what changed.

In any case, you should also be escaping the / characters in your string like so:

return "<img src=\"images\/icons\/icon-alert.gif\"\/>";

slick

8:57 pm on Jul 30, 2008 (gmt 0)

10+ Year Member



Yeah you are right Fotiman except that I didnt have my full image path there such as Images/Icons/Icon-Alert.Gif... As soon as I did that, it worked.

Whats the advantage/purpose of "escaping"? Is it just good practice?

Fotiman

9:10 pm on Jul 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Technically, your browser is supposed to see the / as indicating that you are closing the <script> element. I'm not sure if any of them actually do, but it's similar to why people do things like:

document.write("<scr" + "ipt...

Play it safe... escape that character as it should be. :)

slick

9:18 pm on Jul 30, 2008 (gmt 0)

10+ Year Member



Sound's good then. I'll do it ... as long as it still works and I'm playing by the rules.. :-)