Nayantara Jeyaraj
Nayantara Jeyaraj

Reputation: 2716

File Path in JavaScript for images

This might be the most trivial question but as far as I understand it seems to be correct but I'm unable to view the images as desired. The image shows the file structure and I'm trying to access these from jsGold.js file. It was working fine until I extracted certain files to a new project for cleanup hence I suspect the change in file path may have caused this issue.

File Structure

Code lines in jsGold.js file:

else {
   streamData.push(clickedId, streamName, streamDef);
   var prop = $('<img class="prop" src="../../Images/prop.ico" />').attr('id', i);
   newAgent.text(streamData[nameIndex]).append('<img class="cancel" src="../../Images/delete.ico"/>').append(prop);

The output that I'm getting is:

Output

Desired Output:

Desired Output

Upvotes: 0

Views: 9008

Answers (2)

Nayantara Jeyaraj
Nayantara Jeyaraj

Reputation: 2716

Thanks guys - the following works since I'm opening the file from work.html in views

var prop = $('<img class="prop" src="../Images/prop.ico" />').attr('id', i);

Upvotes: 0

ADreNaLiNe-DJ
ADreNaLiNe-DJ

Reputation: 4919

The relative path of you img tag should be relative to the root of your website, not relative to the folder of your script.

You should use :

var prop = $('<img class="prop" src="/Images/prop.ico" />').attr('id', i);

If you open the page, from Views, directly from your disk and not from a webserver, relative path must relative from the Views folder. So you have to write it like this:

var prop = $('<img class="prop" src="../Images/prop.ico" />').attr('id', i);

Upvotes: 2

Related Questions