¿Cómo obtener la ruta base en jQuery?


window.locationfunciona bien, pero me devuelve el camino completo, absoluto, como http://domain.xyz/punch/lines. Pero solo necesito http://domain.xyz/. ¿Cómo puedo extraer solo esa primera parte? Y ¿cómo puedo hacer que la dinámica, quiero decir que sea siempre el mismo, incluso cuando la ruta del subdirectorio se hace más larga?

Author: leymannx, 2013-08-01

4 answers

Puede obtener el protocolo y el host por separado, y luego unirse a ellos para obtener lo que necesita

window.location.protocol + "//" + window.location.host + "/"

Como nota lateral, window.location.pathname contendría la ruta.

 68
Author: adeneo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-04 12:49:57

Puede usar esta declaración

var baseUrl = document.location.origin;
 23
Author: Peter T.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-03-01 22:52:52

Prueba esto:

location.protocol + "//" + location.host
 2
Author: Dharmesh Patel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-01 11:20:48

Creo que va a estar bien para usted

var base_url = window.location.origin;

var host = window.location.host;

var pathArray = window.location.pathname.split( '/' );
 1
Author: Haris,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-09 06:46:23