javascript - Problem with js intercepting url, do not use # after it
高洛峰
高洛峰 2017-05-19 10:31:45
0
5
729

Currently, the way I know of js to obtain the URL carrying parameters is
window.location.search

But suppose there is a link to www.xxxxxx.com?aaa=bbb&ccc=ddd#ok

What is obtained is?aaa=bbb&ccc=ddd#ok
I don’t want the hash value after this #. Can it be removed? Or do I have to use regular expressions to remove it?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(5)
左手右手慢动作

The

location object has a hash attribute, which stores the string starting with # in the URL, so it is not necessary to use regular replacement. Direct matching replacement can also be used:

var nohash = window.location.href.replace(window.location.hash, '');

Reference: http://www.w3school.com.cn/js...

给我你的怀抱

Can you be sure that only one # symbol appears in the address string? If possible, get the entire address string, and then use string.IndexOf("#") to get the current character position. Then you can get it at will
For example:

String str = www.xxxxx.com?aaa=bbb&ccc=ddd#ok

Then

String url = str.substring(0,str.IndexOf("#"));

----------------------------------Separating line---------- ------------------

仅有的幸福

var str = " www.xxxxxx.com?aaa=bbb&ccc=ddd#ok" ;
var url = str.split('#')[0];

習慣沉默

It should be possible that there is no direct interception function on your mobile phone


It’s better to judge and split it

为情所困
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <script type="text/javascript">
      var url = window.location.herf;
      var url = 'https://www.baidu.com/s?wd=1&rsv_spt=1#notNeed';
      function getQuery(url){
        var temparr = [],
            json = {};
        var queryStr = url.split('?')[1];
        var queryStrNoAnchor = queryStr.substr(0,queryStr.indexOf('#'));
        tempArr = queryStrNoAnchor.split('&');
        var i = 0,
            len = tempArr.length;
        for(; i<len; i++){
          json[tempArr[i].split('=')[0]] = tempArr[i].split('=')[1];
        }
        return json;
      }
      var result = getQuery(url);
      console.log(result);
    </script>
  </body>
</html>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template