0

When I alerted the Drupal.settings.basePath it's just giving me a /. I have a full $base_url in settings.php. which I'm expecting to see instead of /.

How can I get the correct value of the base URL in javascript?

1 Answer 1

6

You need to pass it manually like this.

global $base_url;
drupal_add_js(array('baseUrl' => $base_url), 'setting');

Then call like this Drupal.settings.baseUrl.

Drupal.settings.basePath value is coming from base_path() api. See this documentation about base_path.

4
  • why is it necessary to use global $base_url; before passing the variable ?
    – KTM
    Commented Aug 19, 2015 at 15:44
  • Because it is not available inside a hook by default
    – Gulok
    Commented Aug 19, 2015 at 23:12
  • But i can use $base_url as a variable in the same file though
    – KTM
    Commented Aug 20, 2015 at 6:59
  • 1
    it really works for me
    – WaQaR Ali
    Commented Apr 27, 2017 at 13:30

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.