我见过很多jQuery的例子,参数大小和名称都是未知的。我的url只有一个字符串。
http://example.com?sent=yes
我只是想检测一下。
1.是否存在 "sent"? 2.它是否等于"yes"?
最佳解决方案这里。
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = window.location.search.substring(1),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
}
}
};
而这是你如何使用这个函数的,假设URL是。
http://dummy.com/?technology=jquery&blog=jquerybyexample
。
var tech = getUrlParameter('technology');
var blog = getUrlParameter('blog');
我希望这将会有所帮助。
<script type="text/javascript">
function getParameters() {
var searchString = window.location.search.substring(1),
params = searchString.split("&"),
hash = {};
if (searchString == "") return {};
for (var i = 0; i < params.length; i++) {
var val = params[i].split("=");
hash[unescape(val[0])] = unescape(val[1]);
}
return hash;
}
$(window).load(function() {
var param = getParameters();
if (typeof param.sent !== "undefined") {
// Do something.
}
});
</script>
试试这个的工作演示
API。
inArray
: http://api.jquery.com/jQuery.inArray/这应该会有帮助 :)
代码
var url = "http://myurl.com?sent=yes"
var pieces = url.split("?");
alert(pieces[1] + " ===== " + $.inArray("sent=yes", pieces));