-1

I Have added a script ( JS File ) in Magento 2 admin side in my custom module. It added successfully but how to get Base URL in admin side js file.

I have tried this

Js file path ( app/code/Tym17/AdminSample/view/adminhtml/web/js/adminscript.js )

require([
 'mage/url'
], function (urlBuilder) {
 var customLink = urlBuilder.build('customer/account/login');
console.log(customLink);
});

But it gives me the current page full URL.

My layout file path ( app/code/Tym17/AdminSample/view/adminhtml/layout/adminsample_sampletwo_index.xml )

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<head>
    <link src="Tym17_AdminSample::js/adminscript.js"/>
</head>
<body>
    <referenceContainer name="content">
        <block class="Tym17\AdminSample\Block\Adminhtml\Order" name="Overview" template="Tym17_AdminSample::sample/two.phtml" />
    </referenceContainer>
</body>

How to get only base url like this http://127.0.0.1/magento/

1

3 Answers 3

1
<script>
    require([
     "prototype"
    ], function () {
        window.customUrl = '<?= /** @noEscape */ $block->getUrl('sales/order/index')?>';
    });
</script>

Now you can use Global variable inside your custom js file,

var customUrl = window.customUrl; You can get the backend URL using the above variable in the js file.

4
  • Mark answer as right, it its helpful to you Commented Aug 29, 2020 at 10:49
  • Thanks for your response but When i added this script part in my phtml file and used this customUrl = window.customUrl; in my js file but it gives me undefined @Hamendra
    – Prits
    Commented Aug 29, 2020 at 12:18
  • you need to add like var url = <?php echo $block->getUrl(''); ?> Commented Aug 29, 2020 at 13:29
  • Where in my phtml file i have to add var url = <?php echo $block->getUrl(''); ?> and then try to get url in js file. Am i correct please help ? @Hamendra
    – Prits
    Commented Aug 29, 2020 at 13:51
0

Try this code

require([
   'mage/url'
], function (urlBuilder) {
  var customLink = urlBuilder.build('customer/account/login');
  console.log(customLink);
});

And check more information this link :-

https://www.rakeshjesadiya.com/get-custom-url-in-js-file-magento-2/

THANKS.

6
  • Thanks for your response but it gives me an error : Uncaught Error: Mismatched anonymous define() module: function (urlBuilder) { var customLink = urlBuilder.build('customer/account/login'); console.log(customLink); } will you please help @Mohit
    – Prits
    Commented Aug 29, 2020 at 17:12
  • please share your full code ... Commented Aug 29, 2020 at 17:13
  • Hello @Prits i have update my answer.And i have change defined Instead require Commented Aug 29, 2020 at 17:18
  • It gives me customer/account/login in console. I have updated my adminsample_sampletwo_index.xml and js file. Please check @Mohit
    – Prits
    Commented Aug 29, 2020 at 19:11
  • Please clear your new actually problem ... Commented Aug 30, 2020 at 5:10
0

If you want baseurl in js then can we achieve using this "BASE_URL"

For more please inspect element and find for "BASE_URL"

Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.