5

how we can print base_url in js file (in drupal)?please

2 Answers 2

26

Drupal 7

Drupal.settings.basePath

Drupal 8

drupalSettings.path.baseUrl
Sign up to request clarification or add additional context in comments.

1 Comment

For me the drupalSettings.path.baseUrl prints "/" and not the actual base url (e.g. example.dev) of the website. Do I miss any extra steps?
2

You can use $base_path. See en example on The Drupal JavaScript API under the "Drupal.settings" section.

1 Comment

This would be usefull only if you are dealing with both PHP and Javascript (define your setting in a custom PHP module, load it in Javascript, than access it). For any other custom setting, this is worth it. But for the base path, I think yoavmatchulsky answer is best.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.