Attributo href Anchor

Definizione e uso

href L'attributo imposta o restituisce il collegamento Valore dell'attributo href.

href L'attributo definisce il destinatario del collegamento.

Esempio

Esempio 1

Cambia il destinatario del collegamento (URL):

document.getElementById("myAnchor").href = "https://www.codew3c.com/";

Prova a te stesso

Esempio 2

Ottieni l'URL del collegamento:

var x = document.getElementById("myAnchor").href;

Prova a te stesso

Esempio 3

Un altro esempio su come ottenere l'URL del collegamento (URL relativo):

var x = document.getElementById("myAnchor").href;

Prova a te stesso

Sintassi

Restituisce l'attributo href:

anchorObject.href

Imposta l'attributo href:

anchorObject.href = URL

Valore dell'attributo

Valore Descrizione
URL

Determina l'URL del collegamento.

Valori possibili:

  • URL assoluto - indica un altro sito (ad esempio href="http://www.example.com/index.htm")
  • URL relativo - indica un file all'interno del sito (ad esempio href="index.htm")
  • URL diancora - si riferisce all'ancora nella pagina (ad esempio href="#top"))

Dettagli tecnici

Valore di ritorno: Valore di stringa, che rappresenta l'URL del link. Restituisce l'intero URL, inclusi il protocollo (ad esempio http://).

Supporto dei browser

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Supporto Supporto Supporto Supporto Supporto

Pagine correlate

Manuale di riferimento HTML:Valore dell'attributo href del tag <a> HTML