Comments on: Reading GET variables with JavaScript https://onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/ building stuff on the web with Josh Fraser Tue, 27 Nov 2012 18:22:53 +0000 hourly 1 By: RA2 https://onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/comment-page-1/#comment-4887 Tue, 27 Nov 2012 18:22:53 +0000 https://onlineaspect.com/?p=291#comment-4887 I needed to write a script to grab the GET variables  from a URL and after a bit of looking around I didn't see too many examples that would allow me to easily access  the vars by name.  I wrote the following function that for better or worst get's the job done when passed a URL string that contains GET parameters. It doesn't care how many only that they are there.

function get_url_params(u){
    
          var theURL = u;
          var JS_GET = new Object();
          var splitURL = theURL.split('?');
         
          if(splitURL.length>1){
               var splitVars = splitURL[1].split('&');
               for(i=0; i< splitVars.length; i++){
                    splitPair = splitVars[i].split('=');
                    JS_GET[splitPair[0]] = splitPair[1];
               }//end for
         
               return JS_GET;
         
          }//end if
          else{
              
               return false;
              
          }
    
     }//end get_url_params

If also returns false if there are no variables in the url. 

You could call :
var testVars = get_url_params('http://orangemantis.net?user=foo&name=bar&id=42&#039;);
alert(testVars.id);
alert(testVars.user);
alert(testVars.name);
or any other get parameter name passed to it.

]]>
By: CMH https://onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/comment-page-1/#comment-4789 Thu, 13 Sep 2012 21:35:13 +0000 https://onlineaspect.com/?p=291#comment-4789 The code has some issues. It consistently fails on first run with the regex expression. Good attempt tho.

]]>
By: Fábio - GreenAfter https://onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/comment-page-1/#comment-4711 Sat, 23 Jun 2012 01:23:40 +0000 https://onlineaspect.com/?p=291#comment-4711 In reply to Fábio – GreenAfter.

2nd errata:
return (val = val.replace(/^?/,'&').match(rexp)) ? (typeof val[1] == 'undefined' ? '' : decodeURIComponent(val[1])) : false;

2nd sorry guys…!!
;D
until…

]]>
By: Fábio - GreenAfter https://onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/comment-page-1/#comment-4710 Sat, 23 Jun 2012 00:47:35 +0000 https://onlineaspect.com/?p=291#comment-4710 In reply to Fábio – GreenAfter.

Edit:
$_HTTP_GET_FRIENDLY_URL()

Sorry guys…!
;D
until…

]]>
By: Fábio - GreenAfter https://onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/comment-page-1/#comment-4709 Sat, 23 Jun 2012 00:34:59 +0000 https://onlineaspect.com/?p=291#comment-4709 And when have a friendly url htaccess configured?
I've made a function that get the last variable value. To my current job, works fine.
But to pass more than one variable value, I'm without time and need at this moment.
However I've decided leave the functions here for you improve them to reach work fine with multiple vars and arguments into friendly urls.

[code]
//htaccess friendly url: test with "http://localhost/contact-page&quot;. Note: Will get only the last arguments (value).
function $_HTTP_GET_FREINDLY_URL(lastvalue)
{
var lastvalue = window.location.pathname;
var regxpID = String(lastvalue.match(new RegExp('[a-zA-Z0-9-]+$','g')));

return regxpID;
}

//without htaccess friendly url: test with "http://localhost/?id=contact-page&quot;
function $_HTTP_GET(variable, value)
{
val = (typeof value == null) ? valor = value : window.location.search;
var rexp = new RegExp('&'+variable+'(?:=([^&]*))?(?=&|$)','i');
return (valor = val.replace(/^?/,'&').match(rexp)) ? (typeof val[1] == 'undefined' ? '' : decodeURIComponent(val[1])) : false;
}

//Test with this window.alert() Function
var friendlyURL = ($_HTTP_GET('id') == false) ? $_HTTP_GET_FREINDLY_URL() : $_HTTP_GET('id');

window.alert($_HTTP_GET_FREINDLY_URL());
window.alert($_HTTP_GET('id'));
window.alert($_HTTP_GET('id',$_HTTP_GET_FREINDLY_URL()));

window.alert(friendlyURL);
[/code]

Have a great weekend!
;D
until…

]]>
By: JRO https://onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/comment-page-1/#comment-4569 Wed, 14 Dec 2011 06:00:35 +0000 https://onlineaspect.com/?p=291#comment-4569 This works better:

function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return unescape(pair[1]);
}
}
return false;
}

]]>
By: jerel https://onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/comment-page-1/#comment-4565 Tue, 13 Dec 2011 21:34:11 +0000 https://onlineaspect.com/?p=291#comment-4565 not to lurk, been trying to get this to do anything for hours now. Before i grind my teeth into dust, could someone be so kind as to show any of this working in context?

say like http://www.whatever.com/index.php?var1=hi is the url
then the page w the script outputs var1 and prints hi?

I know php well, new to js, getting a tad crazy over here…

]]>
By: tomasz https://onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/comment-page-1/#comment-3083 Thu, 12 May 2011 07:24:15 +0000 https://onlineaspect.com/?p=291#comment-3083 Don't you prefer?
s = s || window.location.search;
over
s = s ? s : window.location.search;

]]>
By: Alex https://onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/comment-page-1/#comment-3060 Sat, 30 Apr 2011 15:18:06 +0000 https://onlineaspect.com/?p=291#comment-3060 Yep, it doesn't work without escaping the question mark.
Maybe backslashes are autoremoved from the code section as well

]]>
By: Marty https://onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/comment-page-1/#comment-3072 Thu, 28 Apr 2011 01:48:03 +0000 https://onlineaspect.com/?p=291#comment-3072 In reply to @Zakoholic.

You're a god!!! hehehehe, you found the solution, you escaped the question mark 'cause it is a reserved word of regular expressions!!

]]>