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.
-> I'm using a '#' instead of a '&' or '?'!
This is my current 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');
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.
http://relaunch.headonline.de/projekte/#filter=kataloge-database
-> I'm using a '#' instead of a '&' or '?'!
This is my current 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');
Share
Improve this question
asked Apr 19, 2016 at 8:20
JonasJonas
1631 gold badge3 silver badges10 bronze badges
3
-
What output are you expecting? You need all the
key
andvalue
in an array? – Rino Raj Commented Apr 19, 2016 at 8:24 - My expected output is the url parameter, in this case "kataloge-database". -> relaunch.headonline.de/projekte/#filter=kataloge-database – Jonas Commented Apr 19, 2016 at 8:31
- I have updated my answer. Do let me know if you need any change. – Rino Raj Commented Apr 19, 2016 at 8:34
2 Answers
Reset to default 9var 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./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./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Use like this
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);
}