Tengo nombre de dominio para eq.
1) http://www.abc.com/search 2) http://go.abc.com/workObtengo solo el nombre de dominio de la URL anterior
Salida como
1) http://www.abc.com/ 2) http://go.abc.com/¿Como lo puedo hacer?
Puede aprovechar el analizador de URL del navegador utilizando un elemento <a> :
var hostname = $('<a>').prop('href', url).prop('hostname');o sin jQuery:
var a = document.createElement('a'); a.href = url; var hostname = a.hostname;(Este truco es particularmente útil para resolver rutas relativas a la página actual).
Utilice la siguiente función:
function get_hostname(url) { var m = url.match(/^http:\/\/[^/]+/); return m ? m[0] : null; }Úsalo así:
get_hostname("http://example.com/path"); Esto devolverá http://example.com/ como en su salida de ejemplo.
Si solo está intentando obtener el nombre de host de la página actual, use document.location.hostname .
Esto funcionó para mí.
http://tech-blog.maddyzone.com/javascript/get-current-url-javascript-jquery
$(location).attr('host'); www.test.com:8082 $(location).attr('hostname'); www.test.com $(location).attr('port'); 8082 $(location).attr('protocol'); http: $(location).attr('pathname'); index.php $(location).attr('href'); http://www.test.com:8082/index.php#tab2 $(location).attr('hash'); #tab2 $(location).attr('search'); ?foo=123Prueba así.
var hostname = window.location.origin If the URL is "http://example.com/path" then you will get "http://example.com" as the result.Esto no funcionará para dominios locales.
When you have URL like "https://localhost/MyProposal/MyDir/MyTestPage.aspx" and your virtual directory path is "https://localhost/MyProposal/" In such cases, you will get "https://localhost".