简体   繁体   中英

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');
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> 

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM