Atribut href Anchor

Definisi dan penggunaan

href Atur atau kembalikan tautan Nilai atribut href.

href Atribut menentukan tujuan tautan.

Contoh

Contoh 1

Ubah tujuan tautan (URL):

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

Coba sendiri

Contoh 2

Ambil URL tautan:

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

Coba sendiri

Contoh 3

Contoh lainnya bagaimana mengambil URL tautan (URL relatif):

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

Coba sendiri

Sintaks

Kembalikan atribut href:

anchorObject.href

Atur atribut href:

anchorObject.href = URL

Nilai atribut

Nilai Deskripsi
URL

Tentukan URL tautan.

Nilai yang mungkin:

  • URL Absolut - Menunjuk ke situs web lain (contoh href="http://www.example.com/index.htm")
  • URL Relatif - Menunjuk ke berkas di dalam situs web (seperti href="index.htm")
  • URL Tandai - Menunjuk tandai di halaman (seperti href="#top"))

Detil Teknis

Nilai pengembalian: Nilai string, menunjukkan URL tautan. Mengembalikan URL penuh, termasuk protokol (seperti http://).

Dukungan Peramban

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Dukungan Dukungan Dukungan Dukungan Dukungan

Halaman yang berhubungan

Panduan Referensi HTML:Nilai atribut href Tag <a> HTML