How do I get the fragment identifier (value after

2019-01-03 02:15发布

Example:

www.site.com/index.php#hello

Using jQuery, I want to put the value hello in a variable:

var type = …

7条回答
萌系小妹纸
2楼-- · 2019-01-03 02:33

No need for jQuery

var type = window.location.hash.substr(1);
查看更多
Ridiculous、
3楼-- · 2019-01-03 02:34
var url ='www.site.com/index.php#hello';
var type = url.split('#');
var hash = '';
if(type.length > 1)
  hash = type[1];
alert(hash);

Working demo on jsfiddle

查看更多
放荡不羁爱自由
4楼-- · 2019-01-03 02:40

Use the following JavaScript to get the value after hash (#) from a URL. You don't need to use jQuery for that.

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

I have got this code and tutorial from here - How to get hash value from URL using JavaScript

查看更多
爷、活的狠高调
5楼-- · 2019-01-03 02:40

It's very easy. Try the below code

$(document).ready(function(){  
  var hashValue = location.hash;  
  hashValue = hashValue.replace(/^#/, '');  
  //do something with the value here  
});
查看更多
爷、活的狠高调
6楼-- · 2019-01-03 02:45

I had the URL from run time, below gave the correct answer:

let url = "www.site.com/index.php#hello";
alert(url.split('#')[1]);

hope this helps

查看更多
混吃等死
7楼-- · 2019-01-03 02:49

You may do it by using following code:

var url = "www.site.com/index.php#hello";
var hash = url.substring(url.indexOf('#')+1);
alert(hash);

SEE DEMO

查看更多
登录 后发表回答