You can get the custom or base URL of the store in a javascript(JS) file for the frontend area using the 'mage/url'
widgets.
You have to pass the 'mage/url'
widget as a dependency in a define([])
or require([])
function of the javascript file.
You can fetch Custom Base Url for the Backend area using Backend Base Url in the JS file
Code to get URL in the javascript file,
define([ 'mage/url' ], function (urlBuilder) { var customLink = urlBuilder.build('customer/account/login'); console.log(customLink); });
Using the above code snippet, You can fetch the store login page URL.
You can pass any URL path inside the build('')
method of the 'mage/url'
object.
Hi ! ,
thank you for sharing knowledge , i’m trying to get the controller url inside a javascript file exactly like you did , but i have this error “Uncaught Error: Mismatched anonymous define() module: function (urlBuilder) {” do you have any idea why ? the code is just copy , paste for your above code and i’m using this : ‘rateExchange/index/index’ instead of your login url , this url that i’m trying to reach out (http://127.0.0.1/Magento_Extensions/rateExchange/index/index)