0

I need to find url parameter # with value in javascript.

my url is like:

http://rohitazad.com/wealth/tax/how-to-file-your-income-tax-return/newslist/34343443.cms?intenttarget=no&utm_source=newsletter&utm_medium=email&utm_campaign=ETwealth&type=wealth#sid53239948&ncode=23432kjk#%kjimwer

i want to find this value #sid53239948

I find this How can I get query string values in JavaScript?

but how to find this value in url?

Community
  • 1
  • 1
Rohit Azad Malik
  • 31,410
  • 17
  • 69
  • 97

7 Answers7

1

EDIT:
This will filter the sid into the sid-variable wherever you put your hash.

var url_arr = window.location.hash.split('&'),
    sid = '';

url_arr.filter(function(a, b) {
    var tmp_arr = a.split('#')
    for (var i in tmp_arr)
        if (tmp_arr[i].substring(0, 3) == 'sid')
            sid = tmp_arr[i].substring(3, tmp_arr[i].length)
});

console.log(sid) // Will output '53239948'

Old answer:

var hash_array = window.location.hash.split('#');
hash_array.splice(0, 1);
console.log(hash_array);
Jonathan Nielsen
  • 1,442
  • 1
  • 17
  • 31
0

use this plugin that help you to find exact information

https://github.com/allmarkedup/purl

tapos ghosh
  • 2,114
  • 23
  • 37
0

Try this way,

var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
            // console.log(window.location.href);
            // console.log(hashes);
            for(var index = 0; index < hashes.length; index++)
            {
                var  hash = hashes[index].split('=');

                var value=hash[1];
                console.log(value);

            }
Nitin Dhomse
  • 2,524
  • 1
  • 12
  • 24
0

http://www.w3schools.com/jsref/jsref_indexof.asp

Search a string for "welcome":

var str = "Hello world, welcome to the universe.";
var n = str.indexOf("welcome");

The result of n will be:13. Maybe this helps you. In the posted link in your question you see how you get the url. But be careful: indexOf only returns the 1st occurence.

Garamaru
  • 106
  • 1
  • 1
  • 11
0

The post you have referenced is looking for a URL parameter in a string. These are indicated by:

?{param_name}={param_value}

What you are looking for is the anchor part of the URL or the hash.

There is a simple Javascript function for this:

window.location.hash

Will return:

#sid53239948

See reference: http://www.w3schools.com/jsref/prop_loc_hash.asp

However, given a URL that has multiple hashes (like you one you provided), you will need to then parse the output of this function to get the multiple values. For that you will need to split the output:

var hashValue = window.location.hash.substr(1);
var hashParts = hashValue.split("#");

This will return:

['sid53239948', '%kjimwer']
bgallz
  • 111
  • 6
0

Since you have hash values in query params, window.location.hash will not work for you. You can try to create an object of query parameters and then loop over them and if # exists, you can push in a array.

Sample

function getQStoObject(queryString) {
  var o = {};
  queryString.substring(1).split("&").map(function(str) {
    var _tmp = str.split("=");
    if (_tmp[1]) {
      o[_tmp[0]] = _tmp[1];
    }
  });
  return o
}

var url = "http://rohitazad.com/wealth/tax/how-to-file-your-income-tax-return/newslist/34343443.cms?intenttarget=no&utm_source=newsletter&utm_medium=email&utm_campaign=ETwealth&type=wealth#sid53239948&ncode=23432kjk#%kjimwer";

// window.location.search would look like this
var search = "?intenttarget=no&utm_source=newsletter&utm_medium=email&utm_campaign=ETwealth&type=wealth#sid53239948&ncode=23432kjk#%kjimwer";

var result = getQStoObject(search);
console.log(result)

var hashValues = [];
for(var k in result){
  if(result[k].indexOf("#")>-1){
    hashValues.push(result[k].split('#')[1]);
  }
}

console.log(hashValues)

`

Community
  • 1
  • 1
Rajesh
  • 24,354
  • 5
  • 48
  • 79
0

This solution will return you both values following #.

var url = 'http://rohitazad.com/wealth/tax/how-to-file-your-income-tax-return/newslist/34343443.cms?intenttarget=no&utm_source=newsletter&utm_medium=email&utm_campaign=ETwealth&type=wealth#sid53239948&ncode=23432kjk#%kjimwer';
var obj = url.split('?')[1].split('&');
for(var i = 0; i < obj.length; i++) {
  
  var sol = obj[i].split('#');
  if(sol[1]) {console.log(sol[1]);}
}
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
  • Instead of `url.split(?)`, you can use `window.location.search` and create a mimic variable in your code – Rajesh Jul 19 '16 at 09:32