Atribut location.href Window

Definisi dan penggunaan

location.href Pengaturan atribut atau pengembalian URL keseluruhan halaman saat ini.

Contoh

Contoh 1

Ambil URL halaman saat ini:

let url = location.href;

Coba sendiri

Contoh 2

Atur URL halaman saat ini:

location.href = "";

Coba sendiri

Contoh 3

Atur nilai href untuk mengarahkan ke titik di halaman:

location.href = "#top";

Coba sendiri

Contoh 4

Atur nilai href untuk mengarahkan ke alamat surel (akan membuka perangkat lunak dan membuat surel baru):

location.href = "mailto:someone@example.com";

Coba sendiri

Sintaks

Kembalikan atribut href:

location.href

Atur atribut href:

location.href = URL

Nilai atribut

Nilai Deskripsi
URL

URL absolut, contoh:

http://www.example.com/index.html


URL relatif, contoh:

index.html


URL anjut, seperti:

location.href="#top"


Protokol baru, seperti:

ftp://someftpserver.com

mailto:someone@example.com

file://host/path/example.txt

Nilai kembalian

Tipe Deskripsi
String URL penuh halaman, termasuk protokol (seperti https://).

Dukungan Peramban

Semua peramban mendukung location.href

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