1

I know about GET variables and javascript there are many questions, but I do not understand or get them to work.

I have a html formular, and I need to populate a field with the value of the get variable. The url has 2 variables, here an example:

?pid=form.html&id=9869118

This page is a html only, so I cannot use php, but I want to (firstly) alert, the value of id.

I have tried so many different versions of solutions here and from google.

(For example: http://www.onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/

Please help me to understand how its done correctly and save! Please note, I have no jquery either.

Here is what I have tried so far. This is inside the <script> tags inside my form.html

var GETDATA = new Array();
var sGet = window.location.search;
if (sGet)
{
    sGet = sGet.substr(1);
    var sNVPairs = sGet.split("&");
    for (var i = 0; i < sNVPairs.length; i++)
    {
        var sNV = sNVPairs[i].split("=");
        var sName = sNV[0];
        var sValue = sNV[1];
        GETDATA[sName] = sValue;
    }
}
if (GETDATA["id"] != undefined) {
    document.forms.otayhteytta.id.value = GETDATA["id"];
    }
Owl
  • 689
  • 5
  • 15
  • 29

3 Answers3

3

Take a look at this excellent javascript url manipulation library:

http://code.google.com/p/jsuri/

You can do stuff like this:

Getting query param values by name

Returns the first query param value for the key

new Uri('?cat=1&amp;cat=2&amp;cat=3').getQueryParamValue('cat')   // 1

Returns all query param values the key

new Uri('?cat=1&amp;cat=2&amp;cat=3').getQueryParamValues('cat')  // [1, 2, 3]
gitaarik
  • 42,736
  • 12
  • 98
  • 105
  • Thank you but I do not want tu include a library, should go without it or? I have seen so many different answers without a library – Owl May 22 '13 at 07:51
  • @Kaktus The point of using a library is that it has been tested for some time already, while your custom script isn't yet. I think you should consider it seriously, unless you feel quite confortable in debugging / maintaining a side-solution for that. – Frederik.L May 22 '13 at 08:05
  • Thank you! I understand. I just feel that a whole library is somehow "overblown" for a simple variable in a form. But I consider this! thx – Owl May 22 '13 at 08:16
1

You can use a pure JavaScript function for that like so:

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
    results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

And then you can alert the value of 'id' like so:

alert(getParameterByName('id'));

You can check if the parameter exists using a simple 'if' condition:

var id = getParameterByName('id');
if (id != "") {
    alert(id);
}

Source: How can I get query string values in JavaScript?

Community
  • 1
  • 1
Software Guy
  • 3,190
  • 4
  • 21
  • 21
  • and what is `name`, that's getting passed to the function? – painotpi May 22 '13 at 07:48
  • @Kaktus this does NOT require jQuery :) – Software Guy May 22 '13 at 07:53
  • oh sorry in the linked question they were talking about jquery, I try this one – Owl May 22 '13 at 07:54
  • no worries, the linked question's solution actually doesn't rely on jQuery. – Software Guy May 22 '13 at 07:55
  • I tried it like you said, but it alerts me only an empty window? Not even undefined? (console shows no js errors) – Owl May 22 '13 at 07:55
  • Oh sorry @SoftwareGuy my fault! it works! I need only a fallback when the GET id is not set – Owl May 22 '13 at 08:00
  • it works fine on my end. Can you update your question with the code you tried? I will update my answer to add complete HTML file as sample. – Software Guy May 22 '13 at 08:02
  • @SoftwareGuy sorry my fault, it worked I had copied it wrong. I only need to know, if the variable is not set, how can I test it? I tried with: `var id = getParameterByName('id'); if (id!= '')` and `if (id!= undefined)` both did not work – Owl May 22 '13 at 08:07
  • @Kaktus I have updated the code to include a working 'if' check. – Software Guy May 22 '13 at 08:11
0

A simple way to get the GET parameters without using a library:

var parameters = []
var parts = location.search.substr(1).split('&')

for(var part in parts) {
   var splitted = parts[part].split('=')
   parameters[splitted[0]] = splitted[1]
}

Now parameters is an array with the parameter name in the key and the value as the value.

This is a simple solution and may not work for all scenario's.

gitaarik
  • 42,736
  • 12
  • 98
  • 105