Creating an array from Window.location.hash

I am trying to create an array from the window.location.hash variable, but I am losing.

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

Thank.

+5
source share
7 answers
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; 
        });
+2
source

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 that you may need a search property rather than a hash, but I do not know your specific use case.

+4
source

JavaScript :

var my_item = {value[0] : value[1]};

:

var my_item = {};
my_item[value[0]] = value[1];

, , :

[{name: jason}, {age: 23}, {location: pacific}] //array of single keys

, , , form_data['age'], :

form_data :

form_data = {};

:

form_data[value[0]] = value[1];

, :

{name: jason, age: 23, location: pacific} //associative array with properties
+2

Undeclared form_data , , form_data[i] = ... . script , , .

edit - , , , . .

0

, URL-:

http://www.someUrl.com/Index.htm#foo=bob&moo=alice

<!DOCTYPE html>
<html lang="en">
<head>
    <title>hash me long time</title>
</head>
<body>

    <p>Hello World!</p>

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
    <script type="text/javascript">

        $(function () {

            var hash = window.location.hash.replace('#', '').split('&');
            var myArray = new Array();
            for (var x = 0; x < hash.length; x++) {
                var itemArray = hash[x].split('=');
                var item = new Object();
                item.key = itemArray[0];
                item.value = itemArray[1];
                myArray.push(item);
            }

            for (var x = 0; x < myArray.length; x++)
                console.log(myArray[x].key + ' is ' + myArray[x].value);

        });

    </script>
</body>
</html>
0

.

location.hash = location.hash ? location.hash : "#!";
$.each((location.hash ? location.hash.split("#!") : [""])[1].split("&"), (function () {
 y = this.split("=");
 $hash[y[0]] = y[1];
}));

#! #

0

URL:

function readUrlHashParams() {
    var result = {};
    var hashParam = window.location.hash.substring(1);
    var params = hashParam.split("&");

    $.each(params, function (index, set) {
        var paramSet = set.split("=");
        if (typeof (paramSet[1]) !== "undefined") {
            result[paramSet[0]] = decodeURIComponent(paramSet[1]);
        } else {
            result[paramSet[0]] = "";
        }
    });
    return result;
}

URL

http: //localhost/Index.html#Param1=test1&Param2=Test2&Param3=

Result

var test = readUrlHashParams();
console.log(test);

Object {Param1: "test1", Param2: "Test2", Param3: ""}

0
source

All Articles