is it possible GET parameter

M

MZ

Hello!

I have got URL address like this:

http:\\www.ZZZZ.com?param=10

Is it possible to call and read the GET param parameter value in Javascript?

I mean such code in PHP language:

$_GET["param"]

Kindest regards
Marcin
 
D

David Dorward

Hello!

I have got URL address like this:

http:\\www.ZZZZ.com?param=10

That isn't a URL. The slashes are going in the wrong direction.

And please use example.com (or net or org) for examples (unless you're
a representative of KWIKWEB)
Is it possible to call and read the GET param parameter value in Javascript?

You can parse window.location.search. There isn't a built in system
that splits key/value pairs on ampersands and semi-colons and
constructs an object for you.
 
G

Georgi Naumov

Yes.
It is possible. As David Dorward says above you must
parse window.location. If you cannot made this alone
you can use this function.
/**
* This function assumes a url
* or other string as a argument
* and return hash with url-s
* parameters in the type:
* myhash['getparamname'] = getparam val
* usage:
* var $GET = parseWindowLocation(window.location.href);
* alert($GET['valname']);
* will show the value of GET
* variable undefined otherwise.
* @param string aLocation
* @author
* Georgi Naumov
* (e-mail address removed)
*/
function parseWindowLocation(aLocation)
{
var re = /[?&]([^=]+)=([^&]+)/g;
var maches = null;
var urlParts = new Array();
maches = aLocation.match(re);
if(maches != null)
{
for(var i=0;i< maches.length;i++)
{
var urlPart = maches.split("=");
urlParts[urlPart[0].substr(1)] = urlPart[1];
}
}
return urlParts;
}

Good Luck !

David Dorward :
 
K

K.

Uzytkownik "Georgi Naumov said:
Yes.
It is possible. As David Dorward says above you must
parse window.location. If you cannot made this alone
you can use this function.
/**
* This function assumes a url
* or other string as a argument
* and return hash with url-s
* parameters in the type:
* myhash['getparamname'] = getparam val
* usage:
* var $GET = parseWindowLocation(window.location.href);
* alert($GET['valname']);
* will show the value of GET
* variable undefined otherwise.
* @param string aLocation
* @author
* Georgi Naumov
* (e-mail address removed)
*/
function parseWindowLocation(aLocation)
{
var re = /[?&]([^=]+)=([^&]+)/g;
var maches = null;
var urlParts = new Array();
maches = aLocation.match(re);
if(maches != null)
{
for(var i=0;i< maches.length;i++)
{
var urlPart = maches.split("=");
urlParts[urlPart[0].substr(1)] = urlPart[1];
}
}
return urlParts;
}

Good Luck !

David Dorward :
That isn't a URL. The slashes are going in the wrong direction.

And please use example.com (or net or org) for examples (unless you're
a representative of KWIKWEB)


You can parse window.location.search. There isn't a built in system
that splits key/value pairs on ampersands and semi-colons and
constructs an object for you.




Hello!

Thank you for your help.
I have tried this one:

http://adamv.com/dev/javascript/querystring

Some guy from Polish javascript newsgroupds helped me with finding it.

Thank you both for help
Marcin from Poland
 
R

RobG


Please don't top-post, reply below trimmed quotes.
It is possible. As David Dorward says above you must
parse window.location. If you cannot made this alone
you can use this function.
[...]

Try this one, tested in Safari, Firefox and Opera (wrapped for
posting):

function search2obj(){
// Get search string
var s = window.location.search;
var obj = {};

// Return if no values
if (s.length < 2) return obj;

// Remove ?, fix '+' - change to %20
s = s.substring(1).replace(/\+/g,'%20');

// Split into bits
var bits = s.split(/[=&]/);

// Decode & convert to object
for (var i=0, len=bits.length; i<len; i++) {
obj[decodeURIComponent(bits[i++])] =
decodeURIComponent(bits);
}
return obj;
}

report( search2obj() );

// Just for help - show object property/value pairs
function report(obj){
var p, t = [];
for (p in obj){
t.push(p + ': ' + obj[p]);
}
alert(t.join('\n'));
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top