1

I need to dynamically insert an image in my JS code. In my Razor template I have:

@section Includes {  
      <script type="text/javascript">   


        var imgPath = "@Url.Content("~/Content/img/")";
        alert(imgPath);

      </script>

}

Then in my JS I have:

 insertImg = "";
  if (response[i].someFlag == 'Y') {

                        insertImg = "<img src=\"" + imgPath + "/imgToInsert.gif\" width=\"6px\" height=\"10px\" />";
                    }

But it doesn't work - it will not find the image. The image is stored in /Content/img folder...

What am I doing wrong? I am guessing it is because it is mapping the image from Js script..looks like I will have to hardcode it?

2
  • Alert or console.log the following to see if there are too many slashes " + imgPath + "/imgToInsert.gif" Commented Apr 10, 2012 at 23:27
  • That is what it was! can you answer it so i can accept it? Commented Apr 10, 2012 at 23:30

2 Answers 2

1

Alert or console.log the following to see if there are too many slashes " + imgPath + "/imgToInsert.gif"

Sign up to request clarification or add additional context in comments.

Comments

0

It might be advantageous to use the MVC TagBuilder Class to build your image tags instead of using a string concatenations. Lets you build tags dynamically and makes sure they are well-formed. Most likely your tag is not well-formed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.