Javascript 使用 jquery 获取 url 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36712575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get url parameter with jquery
提问by Jonas
I want to read out an url parameter using jquery and bind this one in a variable. I've seen a lot of ways to solve it but definitely no one worked for me.
我想使用 jquery 读出一个 url 参数并将这个参数绑定到一个变量中。我已经看到了很多解决它的方法,但绝对没有人为我工作。
http://relaunch.headonline.de/projekte/#filter=kataloge-database
http://relaunch.headonline.de/projekte/#filter=kataloge-database
-> I'm using a '#' instead of a '&' or '?'!
-> 我使用的是“#”而不是“&”或“?”!
This is my current javascript:
这是我目前的 javascript:
function $_GET(param) {
var vars = {};
window.location.href.replace( location.hash, '' ).replace(
/[?&]+([^=&]+)=?([^&]*)?/gi, // regexp
function( m, key, value ) { // callback
vars[key] = value !== undefined ? value : '';
}
);
if ( param ) {
return vars[param] ? vars[param] : null;
}
return vars;
}
var filter = $_GET('filter');
回答by Rino Raj
var url = window.location.href;
var arguments = url.split('#')[1].split('=');
arguments.shift();
Working Example
工作示例
var url = "http://relaunch.headonline.de/projekte/#filter=kataloge-database";
var arguments = url.split('#')[1].split('=');
arguments.shift();
alert(arguments)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
var url = window.location.href;
var arguments = url.split('#').pop().split('=').pop();
Working Example
工作示例
var url = "http://relaunch.headonline.de/projekte/#filter=kataloge-database";
var arguments = url.split('#').pop().split('=').pop();
alert(arguments)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
回答by Naveen dev
Use like this
像这样使用
http://relaunch.headonline.de/projekte/#filter=kataloge-database
http://relaunch.headonline.de/projekte/#filter=kataloge-database
var searchParams = new URLSearchParams(window.location.search)
if(searchParams.has('#filter') // true {
var param = searchParams.get('sent');
console.log(param);
}