Document.location

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

La propriété en lecture seule Document.location renvoie un objet Location, contenant les informations sur l'URL du document et fournit des moyens pour modifier cette URL ou charger une autre URL.

Bien que Document.location soit un objet Location en lecture seule, vous pouvez lui assigner un DOMString. Cela signifie que vous pouvez dans la plupart des cas utiliser document.location comme s'il s'agissait d'une chaîne de caractères: document.location = 'http://www.example.com' est un synonyme de document.location.href = 'http://www.example.com'.

Pour récupérer uniquement l'URL en tant que chaîne de caractères, la propriété document.URL peut également être utilisée.

Si le document courant n'est pas un contexte de navigation, la valeur renvoyée est null.

Syntaxe

js
locationObj = document.location;
document.location = "http://www.mozilla.org"; // équivalent à document.location.href = 'http://www.mozilla.org'

Exemple

js
console.log(document.location);
// Affiche un string-like
// "http://www.example.com/juicybits.html" dans la console

Spécifications

Specification
HTML
# the-location-interface

Compatibilité des navigateurs

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
location

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
Has more compatibility info.

Voir aussi

  • L'interface de la valeur renvoyée, Location.
  • Une information similaire mais attachée au contexte de navigation, Window.location