English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Location hash attribute

JavaScript Location  Object

hashAttribute sets or returns the anchor part of the URL, including the hash mark (#).

If the URL does not have a fragment identifier, this attribute returns an empty string "".

Note:When using this attribute to set the anchor part, do not include the hash mark (#).

Syntax:

Return hash attribute:

location.hash

Set hash attribute:

location.hash = anchorName
var anchor = document.getElementById("myAnchor");
document.querySelector("#output").innerHTML = anchor.hash;
Test and See‹/›

Browser Compatibility

All browsers fully support the hash attribute:

Attribute
hashYesYesYesYesYes

Attribute Value

ValueDescription
anchorNameA string specifying the anchor part of the URL

Technical Details

Return Value:A string representing the anchor part of the URL, including the hash (#)

More Examples

Set Anchor Part:

var anchor = document.getElementById("myAnchor");
anchor.hash = "newFragement";
Test and See‹/›

Related References

Location Reference:location.href property

Location Reference:location.hostname property

Location Reference:location.pathname property

Location Reference:location.protocol property

JavaScript Location  Object