6

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');
asked Apr 19, 2016 at 8:20
3
  • What output are you expecting? You need all the key and value in an array? 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 Commented Apr 19, 2016 at 8:31
  • I have updated my answer. Do let me know if you need any change. Commented Apr 19, 2016 at 8:34

2 Answers 2

9
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>

answered Apr 19, 2016 at 8:30
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, Rino! :) Works fine!
Glad to here that :)
var arguments = url.split('page=')[1].split('&')[0];
0

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);
}
glennsl
29.2k12 gold badges60 silver badges80 bronze badges
answered Aug 23, 2018 at 6:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.