Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / May 2007



Tip: Looking for answers? Try searching our database.

is it possible GET parameter

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MZ - 30 May 2007 17:02 GMT
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
David Dorward - 30 May 2007 17:28 GMT
> 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.
Georgi Naumov - 30 May 2007 21:59 GMT
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
            * gonaumov@gmail.com
            */
            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[i].split("=");
                                urlParts[urlPart[0].substr(1)] = urlPart[1];
                        }
                    }
                    return urlParts;
            }

Good Luck !

David Dorward       :
> > Hello!
> >
[quoted text clipped - 12 lines]
> that splits key/value pairs on ampersands and semi-colons and
> constructs an object for you.
K. - 31 May 2007 12:38 GMT
> Yes.
> It is possible. As David Dorward says above you must
[quoted text clipped - 53 lines]
>> 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
RobG - 31 May 2007 14:25 GMT
> Yes.

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[i]);
 }
 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'));
}
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.