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.
Frage
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?
Lösung
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.
Andere Tipps
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.