Symfony 2:image paths in javascript file with assetic -


i'm trying make path image js file in symfony2 project using asset this:

   var arr = $('<img src="{{ asset("assets/images/linkarrow.png")}}">').css({     position: 'absolute',     ...     }); 

but i'm having following error

    "networkerror: 404 not found - mywebsite.com/bundles/web/app_dev.php/project/1/assets    /images/linkarrow.png" 

my image under web/assets file.

you cannot use twig syntax in javascript file. if can put script in template file work then. clean way define assets base path variable javascript. example, solve problem can add following code in layout template:

<script>     var assetsbasedir = "{{ asset('assets/') }}" </script> 

nb: make sure define script before loading javascript file, if referencing variable directly in js file

then can use in javascript files like:

var arr = $('<img src="' + assetsbasedir + 'images/linkarrow.png'+ '">').css({     position: 'absolute',     ... }); 

happy coding!


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -