function getE(){
var url_string = String(document.location);
var url_array1 = url_string.split('?');
if(url_array1[1]){
var e_array = url_array1[1].split('&');
for(i=0;i<=20;i++){
if(e_array[i]){
pullE(e_array[i]);
}
}
}
}
function pullE(val){
var useVal = val.split('=');
alert(useVal[0]+" == "+useVal[1]); //display purposes
}
this would alert in order:
name == username
password == password
gender == male
age == 18
where useVal[0] is the element name and useVal[1] is the value.
__________________
public string ConjunctionJunction(string words, string phrases, string clauses)
{
return (String)(words + phrases + clauses);
}
<--- Was I Helpfull? Let me know ---<
doesn't have to be two functions. i just like to break it out into two functions so i can compress the top function and still work with the output.
__________________
public string ConjunctionJunction(string words, string phrases, string clauses)
{
return (String)(words + phrases + clauses);
}
<--- Was I Helpfull? Let me know ---<
function getE(){
var url_string = String(document.location);
var url_array1 = url_string.split('?');
if(url_array1[1]){
var e_array = url_array1[1].split('&');
for(i=0;i<=(e_array.length);i++){
if(e_array[i]){
var useVal = e_array[i].split('=');
alert(useVal[0]+" == "+useVal[1]);
}
}
}
}
__________________
public string ConjunctionJunction(string words, string phrases, string clauses)
{
return (String)(words + phrases + clauses);
}
<--- Was I Helpfull? Let me know ---<
function get(name) {
var q = location.search.substring(1).split(/[=&]/);
for (var j=0; j<q.length; j+=2) {
if (q[j] == name) {
return q[j+1];
}
}
return null;
}
Use get("whatever") to retrieve each parameter.
i'm not see'n it
__________________
public string ConjunctionJunction(string words, string phrases, string clauses)
{
return (String)(words + phrases + clauses);
}
<--- Was I Helpfull? Let me know ---<
Your script did not consider important things such as unescaping the name and value pair and replacing +'s with spaces.
I agree it's not the best method of pulling url vars.
however i specifically wrote it for an application i'm writing, figured other people might find it usefull so... posted it here
__________________
public string ConjunctionJunction(string words, string phrases, string clauses)
{
return (String)(words + phrases + clauses);
}
<--- Was I Helpfull? Let me know ---<
__________________
public string ConjunctionJunction(string words, string phrases, string clauses)
{
return (String)(words + phrases + clauses);
}
<--- Was I Helpfull? Let me know ---<
function get(name) {
var q = unescape(location.search.substring(1)).split(/[=&]/);
for (var j=0; j<q.length; j+=2) {
if (q[j] == name) {
return q[j+1];
}
}
return null;
}
Of course, by "j += 2" you're assuming that the query args would always come in name/value pairs and you wouldn't have single name tokens such as:
?name1=value1&orphantoken&name2=value&...
It might be a better idea to first split by "&", and then by "=", in that order.
Also, since the function is splitting and scanning the entire query string each time a name is queried, it's a bit inefficient (but, hey, this is an interpreted language anyways -- who cares about that much efficiency )
While we're at parsing the entire query string and strolling through the tokens in a for loop, we could populate a global associative array so that we can simply access the value we want through something like g_querystring["name1"].
Code:
function parseQueryString()
{
var obj = new Object();
var nvpairs = location.search.substring(1).split("&");
for (var idx = 0; idx < nvpairs.length; idx++)
{
var tokens = nvpairs[idx].split("=");
obj[unescape(tokens[0])] = tokens.length == 2 ?
unescape(tokens[1]) : undefined;
}
return obj;
}
var g_querystring = parseQueryString();
document.write("a=" + g_querystring["a"] + "<br>");
document.write("b=" + g_querystring["b"] + "<br>");
document.write("c=" + g_querystring["c"] + "<br>");
// Iterate through all name/value pairs
for (var name in g_querystring)
{
document.write(name + "=" + g_querystring[name] + "<br>");
}
One could even go about calling this global object "Request.QueryString" as an ode to ASP
Code:
var Request = new Object();
Request.QueryString = parseQueryString();
alert(Request.QueryString["foo"]);
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
I stand by my suggestion of using ASP to parse the querystrings since an .asp page is used.
But if in case of not using asp (or any server-side language for that matter) and the only choice is javascript, the links I posted in my post #7 are more robust parse querystring scripts as they not only cover decoding of + but also allow multiple values which I think no scripts posted here do.
I stand by my suggestion of using ASP to parse the querystrings since an .asp page is used.
But if in case of not using asp (or any server-side language for that matter) and the only choice is javascript, the links I posted in my post #7 are more robust parse querystring scripts as they not only cover decoding of + but also allow multiple values which I think no scripts posted here do.
hehe
MINE DOES!! it doesn't decode that stuff tho. i call it a trade off (actually i built it for a specific purpose and never have to decode the values i'm passing but i'm passing multiple values so i know mine works.
__________________
public string ConjunctionJunction(string words, string phrases, string clauses)
{
return (String)(words + phrases + clauses);
}
<--- Was I Helpfull? Let me know ---<
Can you guys suggest anything for doing this on a local HTML page. To be more specific...I want to do this on a CD. I've tried rlemon single function example, but for some reason it didnt work. In my own troubleshooting I have tried to alert the varialbe containing the enture URL. Anything after the ? doesnt even show up...nor does the ?
Is this a javascript thing or a browser thing? Can anyonhe help?
Thanks
*******
Nevermind...I believe I solved my own problem. I didn't have file:/// in front of my local path to the page. I was testing with IE and when trying FireFox...it new it was local and put it in for me. When doing it in IE the script worked fine.