Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get url parameter with jquery

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');
like image 404
Jonas Avatar asked Apr 19 '16 08:04

Jonas


People also ask

How can get current URL with parameters in jQuery?

In JavaScript, just use window. location. href, and in JQuery use code $(location). attr('href'), both will return the current URL.

How do I find URL parameters?

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.

What is the use of param () method in jQuery?

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.


1 Answers

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>
like image 114
Rino Raj Avatar answered Oct 14 '22 02:10

Rino Raj