mTuran
mTuran

Reputation: 1834

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.

Upvotes: 5

Views: 10072

Answers (7)

Graz
Graz

Reputation: 71

To get associative array from url hash:

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

Example URL

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

Result

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

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

Upvotes: 2

JamesM-SiteGen
JamesM-SiteGen

Reputation: 820

Perfect Object.

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

If you are not yet using #! you can change it to #

Upvotes: 0

Praveen Prasad
Praveen Prasad

Reputation: 32127

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

Upvotes: 2

Pauan
Pauan

Reputation: 2673

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.

Upvotes: 5

Kobi
Kobi

Reputation: 138137

JavaScript doesn't support the following notation:

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

Try this instead:

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

This will create an array, where each element has a key and a value, for example:

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

Using a hash probably makes more scene in your case, so you can call form_data['age'], and won't have to look though the array:

initialize form_data to an object:

form_data = {};

Add keys directly to it:

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

So the result is:

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

Upvotes: 2

a7drew
a7drew

Reputation: 7811

Here's a sample based on the following 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>

Upvotes: 0

Dagg Nabbit
Dagg Nabbit

Reputation: 76776

Undeclared form_data is not an object, so it can't have any properties, so form_data[i] = ... will fail. Run this script in a decent browser and the console should show you a message amounting to what I just said.

edit - no it won't because the bogus object literal syntax will trip it up first as Kobi mentions. Both issues should be fixed.

Upvotes: 0

Related Questions