Issue
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?
Solution
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.
Answered By - jurgen Answer Checked By - Gilberto Lyons (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.