If a user requests the following address (from another page), I want to scroll down to the contact form area:
.html#contact
How would I check if the URL contains the hash #contact
?
If a user requests the following address (from another page), I want to scroll down to the contact form area:
http://www.example./index.html#contact
How would I check if the URL contains the hash #contact
?
Don't. The browser does that for you. Simply have a name="contact"
attribute and the browser will scroll down to that element automatically.
for instance:
<h2 name="contact">The contact form is below</h2>
<form> ...
You can use this simple code to get the URL hash.
var hash = window.location.hash;
if(hash == "#contact") {
// code
}
Note: this will also return the "#" tag!
url.match(/#contact$/)
should return the matches as an array. Just check if it's not null.
The hash in the URL will map to the node with the same ID value. So in your case the page will auto scroll to div with an idea of #contact
you can use
var urlName = document.location.href;
var hash = urlName.split("#").length;