Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Array From Window.location.hash

i am trying to create array from window.location.hash variable but i am failling.

My code is:

        $.each(window.location.hash.replace("#", "").split("&"), function (i, value) {
            value = value.split("=");

            var my_item = {value[0] : value[1]};
            form_data[i] = my_item; 
        });
        console.log(form_data);

Thanks.

like image 962
mTuran Avatar asked Jul 13 '10 04:07

mTuran


2 Answers

Give this a try:

var hash = window.location.hash.slice(1);
var array = hash.split("&");

var values, form_data = {};

for (var i = 0; i < array.length; i += 1) {
    values = array[i].split("=");
    form_data[values[0]] = values[1];
}

console.log(form_data);

...Of course I suspect you may be wanting the search property, rather than hash, but I don't know your specific use case.

like image 67
Pauan Avatar answered Sep 20 '22 05:09

Pauan


your code is correct only error is

 $.each(window.location.hash.replace("#", "").split("&"), function (i, value) {
            value = value.split("=");
            var _formItem={};
            var my_item={};
            my_item[value[0]]= value[1]; 
            form_data[i] = my_item; 
        });
like image 23
Praveen Prasad Avatar answered Sep 23 '22 05:09

Praveen Prasad