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');
In JavaScript, just use window. location. href, and in JQuery use code $(location). attr('href'), both will return the current URL.
Method 1: Using the URLSearchParams Object The URLSearchParams is an interface used to provide methods that can be used to work with an URL. The URL string is first separated to get only the parameters portion of the URL. The split() method is used on the given URL with the “?” separator.
jQuery param() Method The param() method creates a serialized representation of an array or an object. The serialized values can be used in the URL query string when making an AJAX request.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With