{"id":460,"date":"2019-06-12T03:30:39","date_gmt":"2019-06-12T03:30:39","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/06\/12\/get-magento-2-url-in-js-file\/"},"modified":"2025-05-22T16:35:02","modified_gmt":"2025-05-22T11:05:02","slug":"get-magento-2-url-in-js-file","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-magento-2-url-in-js-file\/","title":{"rendered":"How to Get Magento 2 URL in JS File"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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<strong>&nbsp;<\/strong>and append it with your module controller link from. Well, it is not possible to use the block to get the url.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hence, implement the below method to&nbsp;get Magento 2 URL in JS file:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Get Magento 2 URL in JS File:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">define(\n    [\n        'jquery',\n        'mage\/url',\n    ],\n    function (jQuery,url) {\n        'use strict';\n        return function () {\n            \n            var getUrl = url.build('meetanshi\/meetanshi\/index');\n            console.log(getUrl);\n        }\n    }\n);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-460","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=460"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/460\/revisions"}],"predecessor-version":[{"id":15384,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/460\/revisions\/15384"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}