PHP $_GET but with Javascript - javascript

I am able to get data from a URL with the following structure -
http://mydomain.com/test.php?word=hello
Using PHP I would use the following
$word = $_GET["word"];
Can anyone tell me if it is possible to achieve the same thing but using JavaScript?
Thanks

Yes, it is possible:
function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
Source: Get URL Parameters Using Javascript

You can get these values using window.location.search. You can either roll your own code to parse this or use something like the answers to this question.

Related

how to get the parameter value from the url in javascript? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I get a specific parameter from location.search?
I have a url like the following.
http://localhost/xxx.php?tPath=&pid=37
I want to get the pid=37 but by its name because on time the url is like above and then when page refreshed then the url becomes like the following.
http://localhost/xxx.php?tPath=&action=xxx&pid=37&value=13&fname=aaaa&fone=4321122
So I want to get the pid=37. It might be a function to which I pass the pid as a parameter and it returns its value.
How will I do this?
Take a look at this or follow a solution like this:
function getParam( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
var frank_param = getParam( 'pid' );
Use the following function
function getParameterValue(name)
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null ) return "";
else return results[1];
}
Well, there are three answers already, which all use regex. But a regex is really the wrong tool for this job. As someone famously said, some people, when they have a problem, think, "I know, I'll use a regular expression!" Now they have two problems. Here are some of the reasons those answers are wrong:
If the "name" you're looking for has regex characters in it, you need to escape them, and all of the regexen fail if the URL uses an ampersand in the path part (not RFC-conformant, but some people might do it anyway).
They all fail if the parameter you're looking for has characters that are meaningful in a regex (for example, $).
Oh, and they all fail to account for multiple results (e.g., path?foo=bar&foo=baz), which is perfectly valid and relatively common in querystrings.
This really is a job for plain-ol' string functions, as given in this answer. I'd have written the function a little differently stylistically, but the algorithm is sound.
check this small utility http://jsfiddle.net/gwB2C/2/
usage :
var up = new URLParser();
var urlObj = up.parse('http://localhost/xxx.php?tPath=&action=xxx&pid=37&value=13&fname=aaaa&fone=4321122');
alert(urlObj.params['fname']); //alerts 'aaaa'
value of urlObj :
baseURL: "http://localhost/xxx.php"
params:{
action: "xxx"
fname: "aaaa"
fone: "4321122"
pid: "37"
tPath: ""
value: "13"
}
queryString: "tPath=&action=xxx&pid=37&value=13&fname=aaaa&fone=4321122"

jQuery querystring [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
get querystring with jQuery
How do I get the value of a querystring into a textbox using jQuery?
Lets say the url is http://intranet/page1.php?q=hello
I would like the "hello" to be in the textbox.
In my programming archive I have this function:
function querystring(key) {
var re=new RegExp('(?:\\?|&)'+key+'=(.*?)(?=&|$)','gi');
var r=[], m;
while ((m=re.exec(document.location.search)) != null) r.push(m[1]);
return r;
}
You can use that to get the query string value and put in a textbox:
$('#SomeTextbox').val(querystring('q'));
Use the function listed in the answer to this question:
function getParameterByName( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
And then just do something like this:
var qParam = getParameterByName('q');
$('#mytextbox').val(qParam);

Getting Query Parameter from a URL which inturn has some URL with Query parameters

I've the following URL
http://somesite/somepage.aspx
I pass a query parameter value which has another URL with query parameters like this.
http://somesite/somepage.aspx?pageURL=http://someothersite/someotherpage.aspx?param1=value&source=http://anotheronesite/anotherpage
I need to get the pageURL value as the one in the bold letters. But i'm getting
http://someothersite/someotherpage.aspx?param1=value
and i'm not getting the source param. I'm using the following JavaScript function -
function getParameterByName( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
Any ideas?
You need to use URL encoding to encode the parameter. Otherwise & is treated as reserved character and belongs to the "base URL".
have u considered html url encoding the pageURL parameter?
this would greatly simplify your task

url parameter to save it in JavaScript code

I would like to insert a parameter value (AS A NUMBER) found the url into my javascript code.. So basically i have the following url www.rene-zamm.com/mp-dthanks.asp?gglid=123123123
Now i have the following javascript code in the same page and i want that number in the url to be visible also in the javascript code where i have written querydata..:
<script type="text/javascript">
function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
try {
var querydata = gup("gglid");
var gwoTracker=_gat._getTracker("UA-1639156-3");
gwoTracker._trackPageview("/" + **querydata** + "/goal");
alert(querydata);
}catch(err){}
</script>
For some reason the querydata is showing as text and not as number.. please help
If you're sure that querydata is going to be a number, just use parseInt(querydata) and it will return a number, integer to be exact

Best way to get the value of a given URL parameter with Javascript / jQuery?

Suppose you have a URL:
http://www.example.com/whatever?this=that&where=when
How would you extract the value of the where parameter (in this case when)?
This is what I came up with -- I'm wondering if there's a better solution:
$.fn.url_param_value = function(param) {
var url = $(this).attr('href');
var regex = new RegExp(param + "=");
return $.grep(url.split("?")[1].split("&"), function(a) {
return a.match(regex);
})[0].split("=")[1];
}
use jquery.query and have fun :)
you can simply use:
var w = $.query.get("where");
If you are left without jQuery, here is a function I use:
function urlParam(name, w){
w = w || window;
var rx = new RegExp('[\&|\?]'+name+'=([^\&\#]+)');
var val = w.location.href.match(rx);
return !val ? '':val[1];
}
w is an optional parameter(defaulted to window) if you need to read iframe parameters.
I use this bit of javascript from Netlobo.com
function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
or if you are looking for a plugin, try jQuery URL Parser
*Edit: Hmm, nm I guess TheVillageIdiot's suggestion is the best & easiest for jQuery :P... YAY, I learned something new today :) But you should still check out the plugin, it's nice in that it will return any part of the URL including data from a string.

Categories