Rohit Azad Malik
Rohit Azad Malik

Reputation: 32182

How to find url parameter # with value in javascript

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?

Upvotes: 0

Views: 385

Answers (7)

Jonathan Nielsen
Jonathan Nielsen

Reputation: 1502

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);

Upvotes: 1

Shubham Khatri
Shubham Khatri

Reputation: 281706

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]);}
}

Upvotes: 0

Rajesh
Rajesh

Reputation: 24925

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)

`

Upvotes: 0

bgallz
bgallz

Reputation: 111

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']

Upvotes: 0

Garamaru
Garamaru

Reputation: 116

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.

Upvotes: 0

Nitin Dhomse
Nitin Dhomse

Reputation: 2612

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);

            }

Upvotes: 0

tapos ghosh
tapos ghosh

Reputation: 2202

use this plugin that help you to find exact information

https://github.com/allmarkedup/purl

Upvotes: 0

Related Questions