Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "window.location.href" and "window.location.hash"?

I learned "window.location.hash" new and tried in my jquery code instead of "window.location.href" and both of them gave same results.

Code is here :

window.location.href = ($(e.currentTarget).attr("href")); window.location.hash = ($(e.currentTarget).attr("href")); 

What is the difference between them?

like image 966
kalaba2003 Avatar asked May 21 '12 15:05

kalaba2003


People also ask

What does Window location href mean?

Window Location Href The window.location.href property returns the URL of the current page.

What is Windows location hash?

The Location Hash property in HTML is used to return the anchor part of a URL. It can also be used to set the anchor part of the URL. It returns the string which represents the anchor part of a URL including the hash '#' sign. Syntax: It returns the hash property.

What is the difference between window location and document location?

window. location is read/write on all compliant browsers. document. location is read-only in Internet Explorer (at least), but read/write in Gecko-based browsers (Firefox, SeaMonkey).


2 Answers

For an URL like http://[www.example.com]:80/search?q=devmo#test

hash return the part of the URL that follows the # symbol, including the # symbol. You can listen for the hashchange event to get notified of changes to the hash in supporting browsers.

Returns: #test 

href returns the entire URL.

Returns: http://[www.example.com]:80/search?q=devmo#test 

Read More

like image 141
Selvakumar Arumugam Avatar answered Oct 07 '22 17:10

Selvakumar Arumugam


Test it on for example http://stackoverflow.com/#Page

href = http://stackoverflow.com/#Page 
hash = #Page 
like image 40
Henrik Karlsson Avatar answered Oct 07 '22 19:10

Henrik Karlsson