I need to get a URL for an image stored in my theme (app/design/frontend/MyVendor/MyTheme/web/images/image.png
) from a javascript file (payment.js
).
In PHP I can do it like this:
<?php echo $block->getViewFileUrl('images/image.png') ?>
How can I do this in JavaScript?
I did it by adding variable to window from *.phtml file:
<script>
window.imgpath = '<?php echo $block->getViewFileUrl('images/image.png') ?>';
</script>
and reading that variable from window in *.js:
function someFunction() {
var imgPath = window.imgpath;
}
Actually, in Magento core files I saw examples of such things.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With