0

I have this url index.html#secondPage?name=the%20second%20page

I want to get the value of name ("the second page") using javascript and jquery

thanks

karthikr
  • 97,368
  • 26
  • 197
  • 188
  • You are trying to get the anchor. See http://stackoverflow.com/questions/3552944/how-to-get-the-anchor-from-the-url-using-jquery – sprain Apr 16 '13 at 15:35
  • Hello,you should take the time and search for similar questions here,this question has been asked different times already,you could start here: http://stackoverflow.com/questions/1403888/get-url-parameter-with-jquery – sokie Apr 16 '13 at 15:36
  • This may be an answer: http://stackoverflow.com/questions/1403888/get-url-parameter-with-jquery – Fezzy Apr 16 '13 at 15:37
  • Those aren't quite duplicates since they all use `location.search` which will be empty in the url the poster is asking about. – Paul Apr 16 '13 at 15:38

2 Answers2

0

You can use the code from the answers to this question. The only difference is that you want to parse the location.hash instead of location.search so just change that in whichever answer you choose to go with.

You'll also need to use substr to delete the leading # like:

var hash = window.location.hash.substr(1);

Here is the code from my answer to the question I linked to, with the modification:

function get_query(){
    var url = location.hash.substr(1);
    var qs = url.substring(url.indexOf('?') + 1).split('&');
    for(var i = 0, result = {}; i < qs.length; i++){
        qs[i] = qs[i].split('=');
        result[qs[i][0]] = decodeURIComponent(qs[i][1]);
    }
    return result;
}

You can use it like:

var secondPage = get_query()['name']; // 'the second page'
Community
  • 1
  • 1
Paul
  • 139,544
  • 27
  • 275
  • 264
0

Try like below, It will help you

Fiddle : http://jsfiddle.net/RYh7U/144/

Javascript :

function GetURLValue (sKey) {
  return unescape("index.html#secondPage?name=the%20second%20page".replace(new RegExp("^(?:.*[&\\?]" + escape(sKey).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));
}

alert(GetURLValue("name"));
Pandian
  • 8,848
  • 2
  • 23
  • 33