0

i have a link:

<a href="?
processor=<?php echo $value['processor']; ?>&
auth_code=<?php echo $value['auth_code']; ?>
" id="buttonDetails">View Detail</a>

and a function:

function getUrlVars(parsedUrl) {
var vars = {};
parsedUrl.replace(/[?&]+([^=&]+)=([^&]*)/gi,
        function(m, key, value) {
            vars[key] = value;
        });
return vars;
}

what i am doing is passing the link to this function;

var url = $('#buttonDetails').attr('href'); // "?processor=25&auth_code=12"
var first = getUrlVars(url);
alert (first );

this alert will give me [object Object]

any ideas what i am doing wrong?

edit: another way would be to:

var first = getUrlVars()["processor"];
alert(processor);

but this doesn't work as well

edit:

the links are created in a php loop:

foreach($test as Value){
 // here is the link being created and the result is more links.
}

also i changed the id to class for the link.

and i'm doing:

$('.buttonDetails').each(function(){
var parsed = getUrlVars($(this).attr('href'));
console.log(parsed['processor']);

});

this will give me undefined

this console.log(parsed); will give me multiple objects with the data inside them. but when i try to get some specifics from inside the object i get undefined

why would this not work?

thanks again

6
  • alert(getUrlVars(url)["processor"])? Commented Nov 30, 2011 at 21:03
  • No error here. first is an object because you return one: var vars = {}. Alerting an object alerts [object Object] Commented Nov 30, 2011 at 21:06
  • my function needs a var to be passed to it Commented Nov 30, 2011 at 21:06
  • Related article: jquery-howto.blogspot.com/2009/09/… Commented Nov 30, 2011 at 21:07
  • Are you sure that this is putting the url to element? Commented Nov 30, 2011 at 21:20

3 Answers 3

3

In this example:

var url = $('#buttonDetails').attr('href'); // "?processor=25&auth_code=12"
var first = getUrlVars(url);
alert (first );

The reason the alert gives you [object Object] is because that's what first is:

Object
    auth_code: "12"
    processor: "25"

Your second example doesn't work because you are not passing a value to the getUrlVars function. Try this:

var url = $('#buttonDetails').attr('href'), // "?processor=25&auth_code=12"
    parsed = getUrlVars(url);

alert(parsed['processor']);
Sign up to request clarification or add additional context in comments.

5 Comments

@Patrioticcow, are you sure you didn't mistype the 'processor' in either the url or the parsed['processor'] part? Because this works perfectly for me. jsFiddle demo
It's working fine here. You've got something wrong somewhere.. can you provide a pastebin link with the full code? I'll try to help.
Also, I'd just like to specify that the reason he gets [object Object] is because the object's toString function (Object.prototype.toString because it wasn't overridden) always returns "[object Object]". I've found this useful to know in some situations.
wired, it doesn't work on my pc... but i see it working in the jsfiddle
put your code on jsfiddle.net or jsbin.com and link it, also feel free to try my solutions :)
0

jQuery doesn't have a built in function. I use this: http://www.netlobo.com/url_query_string_javascript.html

Comments

0

Possible solutions:

var first = getUrlVars(url)["processor"];
alert(first); // not alert(processor)

or

var first = getUrlVars(url);
alert (first.processor); // or alert(first["processor"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.