Window Location

Date de création : 2022/03/27 14:12

 

JavaScript vous propose de nombreuses méthodes pour afficher l'URL actuelle dans la barre d'adresse du navigateur Web.

Vous pouvez utiliser la propriété d'objet Location de l'objet Window pour obtenir ces détails.

console.log(' href => ' + window.location.href);
console.log(' host => ' + window.location.host);
console.log(' hostname => ' + window.location.hostname);
console.log(' port => ' + window.location.port);
console.log(' protocol => ' + window.location.protocol);
console.log(' pathname => ' + window.location.pathname);
console.log(' hashpathname => ' + window.location.hash);
console.log(' search=> ' + window.location.search);

On peut donc extraire une adresse d'une string:

url = monUrl.split(":");

qui va nous donner un tableau avec deux éléments:

  1. le protocol http ou https
  2. le domaine avec ces dossiers et fichiers //monSiteWeb.com/mondosier/index.html

il suffit de venir concaténer window.location.protocol devant

  baseUrl = window.location.protocol + this.url[1] + "/mesDossiers/mesFichiers";

Cette page a pour source partielle ou intégrale la page suivante:


Page dans la catégorie: