
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
HTML DOM Location hash Property
The Location hash property returns/appends a string value (anchor part) to a URL. Anchor part is prefixed with ‘#’ automatically and then appended.
Syntax
Following is the syntax −
- Returning value of the hash property
location.hash
- Value of the hash property set
location.hash = ‘string’
Example
Let us see an example for Location hash property −
<!DOCTYPE html> <html> <head> <title>Location hash</title> <style> form { width:70%; margin: 0 auto; text-align: center; } * { padding: 2px; margin:5px; } input[type="button"] { border-radius: 10px; } </style> </head> <body> <form> <fieldset> <legend>Location-hash</legend> <label for="urlSelect">Current URL:</label> <input type="url" size="27" id="urlSelect" value="https://www.example.com/"> <input type="text" id="textSelect" placeholder="hash value..."> <input type="button" onclick="setHashValue()" value="Go To URL"> <div id="divDisplay"></div> </fieldset> </form> <script> var divDisplay = document.getElementById("divDisplay"); var urlSelect = document.getElementById("urlSelect"); var textSelect = document.getElementById("textSelect"); function setHashValue() { if(textSelect.value !== ''){ location.hash = textSelect.value; urlSelect.value += location.hash; divDisplay.textContent = 'Page loaded. Current URL active'; } else { divDisplay.textContent = 'Please provide a hash value'; } } </script> </body> </html>
Output
This will produce the following output −
Before clicking ‘Go To Url’ button −
After clicking ‘Go To Url’ button with hash value set −
- Related Articles
- HTML DOM Anchor hash Property
- HTML DOM KeyboardEvent location Property
- HTML DOM Location host Property
- HTML DOM Location hostname Property
- HTML DOM Location href Property
- HTML DOM Location origin Property
- HTML DOM Location pathname Property
- HTML DOM Location port Property
- HTML DOM Location protocol Property
- HTML DOM Location search Property
- HTML DOM Location reload() method
- HTML DOM Location replace() method
- HTML DOM tagName Property
- HTML DOM accessKey Property
- HTML DOM activeElement Property

Advertisements