jquery, how to get URL GET variables in javascript?

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

Answers


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']);

Need Your Help

Why string parameter shares the same address with the argument

c++ string parameters copy arguments

I wrote a function using a string object as the parameter:

QueryOver by alias with NHibernate is using property name not column mapping

c# nhibernate fluent-nhibernate

I'm having trouble specifying the QueryOver order by clause with an object whose property name does not match the column name.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.