简体   繁体   中英

How do you read a hash with an “&” sign in the URL?

I am building a dynamic website using jQuery and Ajax. I want to read the id after the hash in the following URL:

something.php#type=abc&id=123

My current code to read hash value is:

var hash = location.hash.substr(1);
alert(hash);

You can do something like this:-

var hash = window.location.hash.substr(1);
var searchParams = new URLSearchParams(hash);
if(searchParams.has('id')) {
  var id = searchParams.get('id');
  console.log(id);
}

And same for other params if any. Hope this helps.

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