-2

I want to get current page URL using javascript ,Any one have suggestions ?

Also i need to get the content from the hashtag For ex: xxxx.com/#21 i need to get that 21 in the javascript

markusthoemmes
  • 3,030
  • 13
  • 23
Logesh.C
  • 9
  • 3

3 Answers3

2

Use next:

HREF = document.location.href
HASH = document.location.hash

http://www.w3schools.com/jsref/prop_loc_href.asp
http://www.w3schools.com/jsref/prop_loc_hash.asp

Dima Pog
  • 152
  • 4
  • 1
    Some links to documentation would definitely improve answer quality. – J0HN Feb 06 '14 at 11:10
  • The better member is `window.location`, not `document.location`. Use w3.org instead of stupid w3schools with wrong information. – Daniel W. Feb 06 '14 at 11:21
1

To get the entire current URL, use:

var currentURL = window.location;

Then to get the hash, without the # itself, use:

var trimmedHash = window.location.hash.substr(1);
// returns `21` from `xxxx.com/#21`

window.location.hash returns #21, and substr(1) returns all but the first character of that string, thus removing the #.

Josh Harrison
  • 5,842
  • 1
  • 28
  • 44
0

try:

window.location.hash // will result #21
window.location.hash.substr(1) // will result 21

hope that helps.

geevee
  • 5,281
  • 5
  • 29
  • 48