How to get the base path in JS I need the base path in my custom.js file. I have added this custom.js file in my MYTHEME.info file.

In custom.js file I'm trying to print 

var baseUrl = Drupal.settings.basePath;
console.log(baseUrl);


Gives me empty value.
This variable Drupal.settings.basePath is provided by drupal.js, just make sure the page includes this js file.(right click view source code to check)

If this did not work, you can create your own variable and use it on the client side:


You can add your custom.js file into your file .info
scripts[] = custom.js or drupal_add_js(drupal_get_path('module','my_module'). '/custom.js');
Use preprocess function in a custom module



    function moduleName_preprocess(&$vars){
             global $base_url;
             global $user;
          drupal_add_js(array('moduleName' =>
      array(
        'basepath'=> $base_url,
        'another_variable' => $user->uid
           ),'setting');
        }



access the variable into your js file :



  var basepath = settings.moduleName.basepath; 



that should be it!