Veremos cómo obtener la URL de la página en que nos encontramos con Javascript y JQuery.
Javascript
Dentro de Javascript tenemos un objeto llamado window.location que nos permite obtener la URL de la página donde nos encontremos.
https://www.localhost.com/algundirectorio/index.html?variable=valor#18
var Url = window.location;
Pero podemos separar nuestra URL por si queremos saber solamente una parte de esta.
| window.location | https://www.localhost.com/algundirectorio/index.html?variable=valor#18 |
| window.location.protocol | https:// |
| window.location.hostname | www.localhost.com |
| window.location.pathname | /index.html?variable=valor#18 |
| window.location.hash | #18 |
| window.location.href | https://www.localhost.com/algundirectorio/index.html?variable=valor#18 |
| window.location.search | ?variable=valor#18 |
| window.location.port | si lo tuviera como :8080 |
JQuery
En JQuery para obtener la URL usamos:
var Url = $(location).attr('href');
| href | https://www.localhost.com/algundirectorio/index.html?variable=valor#18 |
| protocol | https:// |
| hostname | www.localhost.com |
| pathname | /index.html?variable=valor#18 |
| hash | #18 |
| href | https://www.localhost.com/algundirectorio/index.html?variable=valor#18 |
| search | ?variable=valor#18 |
| port | si lo tuviera como :8080 |
![]()