1

I have image url in js variable /w/s/wsh10-black_main.jpg.

Question: How can I get media cache url in js, like: https://mydomain.test/media/catalog/product/cache/c687aa7517cf01e65c009f6943c2b1e9/?

As result I want get full url, like https://mydomain.test/media/catalog/product/cache/c687aa7517cf01e65c009f6943c2b1e9/w/s/wsh10-black_main.jpg

Vasyl Zhuryk
  • 1,228
  • 10
  • 23

1 Answers1

0

You need to get base url in order to have the complete url you want. You can do this by defining a function that returns base url so,

<?php $baseUrl = $this->getBaseUrl() ; ?>
    <script type="text/javascript">
        function getBaseUrl() { return '<?php echo $baseUrl; ?>'; } 
    </script>

then use it in JavaScript:

var urrl = 'http://'+getBaseUrl()+'/w/s/wsh10-black_main.jpg';

You can also refer to this link https://magento.stackexchange.com/questions/60111/get-base-url-in-js-magento

Hope it helps!

surbhi agr
  • 151
  • 1
  • 7
  • But in this case urrl = `https://mydomain.test/w/s/wsh10-black_main.jpg`, but I need url with cache code, like this: `https://mydomain.test/media/catalog/product/cache/c687aa7517cf01e65c009f6943c2b1e9/w/s/wsh10-black_main.jpg` – Vasyl Zhuryk Oct 22 '18 at 18:34
  • Okay in that case you will have to add image url. I think the following link can help you with that https://magento.stackexchange.com/questions/124371/how-to-get-url-for-static-image-file-in-magento2-javascript-files – surbhi agr Oct 23 '18 at 04:49
  • For call getViewFileUrl() I have to put image path. But in my case, I just need path to files (without image). Example: `media/catalog/product/cache/c687aa7517cf01e65c009f6943c2b1e9/` – Vasyl Zhuryk Oct 24 '18 at 17:35