0

I just need to get the value of idi variable from the url.

Everything works fine in the below code except splitting "&".

Example: if this is the url: www.site.com/page?idi=22&selse=88

i want to get the idi parameter's value from the url and print it as a value of a hidden text input field.

<script>
q=window.location.search.substring(1);
idi="idi";
sq=q.split("&");
l=sq.length;
for(i=0;i<=l;i++)
{  

qv=sq[i].split("="); 
if(qv[0]==idi)
{
qvr=qv[1].Split('&');
document.write(<input type='text' value='"+qvr[0]+"' name='get-idi'/>");
break;

}
}

</script>

Improve or help me form a script for this. Thank you!

Saravanan
  • 23
  • 6

4 Answers4

0

Here, this should help you out:

function getParams(url) {

  var result = {}
    , params = /[?&]([^?&=]+)=([^?&]+)?/g;

  decodeURIComponent(url).replace(params, function(_,key,val) {
    result[key] = isNaN(val) ? val : +val;
  });

  return result;
}

console.log(getParams('www.site.com/page?idi=22&selse=88'));
//^= {idi: 22, selse: 88}
elclanrs
  • 92,861
  • 21
  • 134
  • 171
0

This should work fine

var GET = function(key){
    var str = window.location.search.substr(1);
    var array = str.split('&');
    var map = {};

    for (var i = 0; i < array.length; ++i) {
        var temp = array[i].split('=');
        map[temp[0]] = decodeURIComponent(temp[1]);
    }

    delete map[''];

    return (key !== undefined)? map[key] : map;
}
Mmmh mmh
  • 5,334
  • 3
  • 21
  • 29
0

If you're only trying to get one particular value idi as you indicate, then you can use a simple regular expression:

if ((match = window.location.match(/[?&]idi=(\d+)/)) != null){
    alert('IDI: ' + match[1]);
}

The regular expression captures any digits after idi= into match[1]

itsmejodie
  • 4,148
  • 1
  • 18
  • 20
0

Try this,

function getUrlParams(url, key) {
    var vars = [], hash;
        var hashes = url.slice(url.indexOf('?') + 1).split('&');
        for (var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars[key];
    }

alert(getUrlParams("www.site.com/page?idi=22&selse=88", "idi"));

Demo: http://jsfiddle.net/EscBg/

Or you can use https://github.com/allmarkedup/jQuery-URL-Parser

The JQuery plugin do the same job, for example to retrieve the value of idi query string param, you can use

$.url().param('idi');

Update: if you want to use window.location.href try this

function getUrlParams(key) {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars[key];
}
Satpal
  • 132,252
  • 13
  • 159
  • 168
  • Thanks but. qs=window.location.href; alert(getUrlParams(qs, "idi")) this returns idi=22&selse i want only 22 not 22&selse.. – Saravanan Jun 03 '13 at 09:51