can-globals
Version:
This module provides a dependency injection container. Modules may define a key and specify a default value (which can be static, cached lazy, or dynamic lazy), but other code can set and reset the value as needed. There is also an event system, for alert
44 lines (38 loc) • 1.33 kB
JavaScript
;
var globals = require('../can-globals-instance');
require('../global/global');
require('../document/document');
/**
* @module {function} can-globals/base-url/base-url base-url
* @parent can-globals/modules
*
* @signature `baseUrl(optionalBaseUrlToSet)`
*
* Get and/or set the "base" (containing path) of the document.
*
* ```js
* var baseUrl = require("can-globals/base-url/base-url");
*
* console.log(baseUrl()); // -> "http://localhost:8080"
* console.log(baseUrl(baseUrl() + "/foo/bar")); // -> "http://localhost:8080/foo/bar"
* console.log(baseUrl()); // -> "http://localhost:8080/foo/bar"
* ```
*
* @param {String} setUrl An optional base url to override reading the base URL from the known path.
*
* @return {String} Returns the set or computed base URL
*/
globals.define('base-url', function(){
var global = globals.getKeyValue('global');
var domDocument = globals.getKeyValue('document');
if (domDocument && 'baseURI' in domDocument) {
return domDocument.baseURI;
} else if(global.location) {
var href = global.location.href;
var lastSlash = href.lastIndexOf("/");
return lastSlash !== -1 ? href.substr(0, lastSlash) : href;
} else if(typeof process !== "undefined") {
return process.cwd();
}
});
module.exports = globals.makeExport('base-url');