[英]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]
此警报将给我[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: 链接是在php循环中创建的:
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. 我也改变了id
为class
的链接。
and i'm doing: 我正在做:
$('.buttonDetails').each(function(){
var parsed = getUrlVars($(this).attr('href'));
console.log(parsed['processor']);
});
this will give me undefined
这会让我undefined
this console.log(parsed);
这个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
但是当我尝试从对象内部获取一些细节时,我却无法undefined
why would this not work? 为什么这不起作用?
thanks again 再次感谢
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 Object]
的原因是,这first
是:
Object
auth_code: "12"
processor: "25"
Your second example doesn't work because you are not passing a value to the getUrlVars
function. 您的第二个示例不起作用,因为您没有将值传递给getUrlVars
函数。 Try this: 尝试这个:
var url = $('#buttonDetails').attr('href'), // "?processor=25&auth_code=12"
parsed = getUrlVars(url);
alert(parsed['processor']);
jQuery doesn't have a built in function. jQuery没有内置函数。 I use this: http://www.netlobo.com/url_query_string_javascript.html 我用这个: http : //www.netlobo.com/url_query_string_javascript.html
Possible solutions: 可能的解决方案:
var first = getUrlVars(url)["processor"];
alert(first); // not alert(processor)
or 要么
var first = getUrlVars(url);
alert (first.processor); // or alert(first["processor"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.