Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

642
Views
¿Cómo obtener el nombre de dominio de la URL usando jquery ...?

Tengo nombre de dominio para eq.

 1) http://www.abc.com/search 2) http://go.abc.com/work

Obtengo 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?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

en un navegador

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).

Fuera de un navegador (y probablemente más eficientemente):

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.

Nombre de host de la página actual

Si solo está intentando obtener el nombre de host de la página actual, use document.location.hostname .

over 4 years ago · Santiago Trujillo Report

0

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=123
over 4 years ago · Santiago Trujillo Report

0

Prueba 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".
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!