문제

I have a line of javascript within my view in my rails app:

<script>

$(this).find("img").attr("src", "/assets/img5.png"); 

</script>

I have the image path hard coded as you can see. This is working fine locally, but is not working well on production because i'm serving the assets through a cdn.

How do I include an image tag without messing up the javascript?

도움이 되었습니까?

해결책

After playing around a bit, I found something that works:

<script>

$(this).find("img").attr("src", '<%= asset_path 'img5.png' %>'); 

</script>

Looks like asset_path gets the job done.

다른 팁

I would be using soemthing like

jQuery('img[src*="signup_arrow.png"]')

without changing much of the current code. Here src* indicates that "signup_arrow.png" can be a substring of the image src.

Typically CDN urls would be http://cdn.example.com/signup_arrow.png?13304 to using the selector to find the given image name as substring helps.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top