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"];
    }
5
  • A useful article: papermashup.com/read-url-get-variables-withjavascript Commented May 22, 2013 at 7:44
  • and "when" are you try to get the GET variables? do you have an even or something? Commented May 22, 2013 at 7:45
  • Thanks @aug I tried this one too, did not work either. Commented May 22, 2013 at 7:49
  • @badZoke all I have is this code, I thought the window.location.search does the job? Commented May 22, 2013 at 7:50
  • I think he's just trying to get the parameters passed to the page in Javascript, even if the OP does not do anything with them server side. Commented May 22, 2013 at 8:01

3 Answers 3

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]
Sign up to request clarification or add additional context in comments.

3 Comments

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
@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.
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
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?

9 Comments

and what is name, that's getting passed to the function?
@Kaktus this does NOT require jQuery :)
oh sorry in the linked question they were talking about jquery, I try this one
no worries, the linked question's solution actually doesn't rely on jQuery.
I tried it like you said, but it alerts me only an empty window? Not even undefined? (console shows no js errors)
|
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.

1 Comment

Thank you! This may be useful too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.