How to Get Magento 2 URL in JS File

How to Get Magento 2 URL in JS File

The post shows the method to get Magento 2 URL in the JS file. For example, you are stuck in a situation where you want to get url from .js file and append it with your module controller link from. Well, it is not possible to use the block to get the url.

You may require to get the URL in JS file and it is not recommended to use the static URL as with time, the URL key may change and the static value will give you the error.

Hence, implement the below method to get Magento 2 URL in JS file:

Method to Get Magento 2 URL in JS File:

Any doubts on the topic are welcome in the comments section below.

Thanks.

Get Weekly Updates

Never miss Magento tips, tricks, tutorials, and news.

Thank you for subscribing.

Something went wrong.

4.9
(based on 8 Reviews)

Sanjay is a co-founder at Meetanshi. He is a Certified Magento Developer who loves creating Magento E-commerce solutions. Owing to his contributions in Magento Forums and posting solutions, he is among the top 50 contributors of the Magento community in 2019. When he is not engrossed with anything related to Magento, he loves to play cricket.

Leave a reply:

Your email address will not be published.