如何编辑 URL 中锚点的部分内容

发布于 2024-09-30 12:13:21 字数 1419 浏览 1 评论 0原文

我有获取锚点一部分的代码:

function _get_part(queryString, name) {
  var match = '&' + name + '=';
  var i = queryString.indexOf(match);

  if(i < 0) {
    match = name + '=';
    if(queryString.slice(0, match.length) == match)
      i = 0;
  }

  if(i > -1) {
    i += match.length;
    return queryString.slice(i, queryString.indexOf('&', i) >>> 0);
  }
};

function get_location_hash() {
    return window.location.hash.substr(2);
}

function get_part(name) {
    return _get_part(get_location_hash(), name);
}

我需要一个函数来更改锚点的一部分(如果该部分存在),或者添加一个部分(如果不存在)。

此时我使用以下代码:

function set_part(queryString, key, value) {
  var oldv = key + '=' + get_part(key);
  var newv = key + '=' + value;
  window.location.hash = '/' + queryString.replace(oldv, newv);
}

但是如果锚点的部分不存在,则锚点不会改变。

URL 格式: ...page/#/var1=blablabla&var2=var2text&gghh=edere

锚 - #/var1=blablabla&var2=var2text&gghh=edere

抱歉我的英语不好。

多谢!

更新:

太棒了,非常感谢! 只有一个问题:我加载页面时没有任何锚点: .../页/ nex 使用此代码:

set_part(get_location_hash(), 'filter', 'data');
set_part(get_location_hash(), 'filter2', 'data2');
set_part(get_location_hash(), 'fdgfg', 'fdgfdg');
alert(get_part('fdgfg'));

并接收 .../page/#/=&filter=data&filter2=data2&fdgfg=fdgfdg

如何删除第一个“=”符号?

I have code for get part of anchor:

function _get_part(queryString, name) {
  var match = '&' + name + '=';
  var i = queryString.indexOf(match);

  if(i < 0) {
    match = name + '=';
    if(queryString.slice(0, match.length) == match)
      i = 0;
  }

  if(i > -1) {
    i += match.length;
    return queryString.slice(i, queryString.indexOf('&', i) >>> 0);
  }
};

function get_location_hash() {
    return window.location.hash.substr(2);
}

function get_part(name) {
    return _get_part(get_location_hash(), name);
}

I need a function for changing a part of the anchor, if this part exists, or add a part if it does not exist.

At this time I use the following code:

function set_part(queryString, key, value) {
  var oldv = key + '=' + get_part(key);
  var newv = key + '=' + value;
  window.location.hash = '/' + queryString.replace(oldv, newv);
}

But if the part of the anchor does not exist, the anchor doesn't change.

URL format: ...page/#/var1=blablabla&var2=var2text&gghh=edere

Anchor - #/var1=blablabla&var2=var2text&gghh=edere

Sorry about my English.

Thanks a lot!

update:

it awesome, thank you very much!
only one problem: i load page withoud any anchors:
.../page/
nex use this code:

set_part(get_location_hash(), 'filter', 'data');
set_part(get_location_hash(), 'filter2', 'data2');
set_part(get_location_hash(), 'fdgfg', 'fdgfdg');
alert(get_part('fdgfg'));

and receive .../page/#/=&filter=data&filter2=data2&fdgfg=fdgfdg

how to delete first '=' symbol?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离线来电— 2024-10-07 12:13:21

如果 key 已存在于 url 哈希中,则您的函数可以正常工作。

例如,如果您的 url 是:

http://example.com/whatever.html#/var1=blablabla&var2=var2text&gghh=edere

那么,像这样调用 set_part()

set_part(get_location_hash(), 'var2', 'bar');

将会更改哈希值,如下所示:

http://example.com/whatever.html#//var1=blablabla&var2=bar&gghh=edere

注意:它正确更改了 var2 的值。但它确实在开头添加了额外的斜杠。

问题是,如果以前不存在这样的键,它不会添加参数。如果您想这样做,那么我建议您执行以下操作:

function _parseQueryString( queryString ) {
    var pairs = queryString.split('&');
    var params = {};
    for(var i=0; i<pairs.length; ++i ) {
       pairs[i] = pairs[i].trim();
       if( pairs[i] == '' ) continue;

        var parts = pairs[i].split('=');
        if( parts.length == 0 ) continue;
        var name = parts.shift();
        var value = '';
        while(parts.length) {
            value += parts.shift();
        }
        params[name]=value;
    }
    return params;
}

function _buildQueryString( params ) {
    var queryString = "";
    for( var i in params ) {
        if( queryString.length > 0 ) queryString += "&";
        queryString += (i + '=' +params[i]);
    }
    return queryString;
}

function set_part(queryString, key, value) {
    /* first remove the leading '#/', if any */
    if( queryString.indexOf('#')==0 ) {
        queryString = queryString.substring(1);
    }
    if( queryString.indexOf('/')==0 ) {
        queryString = queryString.substring(1);
    }

   /* now parse the hash (in queryString format) into 
      an object containing all the parts */
   var params = _parseQueryString(queryString);

   /* finally, just set the value in the params object, 
      and rebuild the query string, adding 
      in the slash again 
   */
   params[key]=value;
   window.location.hash = '/' + _buildQueryString(params);
}

现在,如果您的页面的 url 是

http://example.com/whatever.html#/var1=blablabla&var2=var2text&gghh=edere

并且您这样称呼它:

alert( 'hash before: '+window.location.hash );

set_part( window.location.hash, 'var2', 'bar');
set_part( window.location.hash, 'foo', 'foovalue');

alert( 'hash after: '+window.location.hash );

那么您可以看到两种表单都可以正常工作。

您可以在 jsfiddle 上查看此示例

Your functions work correctly if the key is already present in the url hash.

For example, if your url is:

http://example.com/whatever.html#/var1=blablabla&var2=var2text&gghh=edere

Then, calling set_part() like this:

set_part(get_location_hash(), 'var2', 'bar');

will change the hash as follows:

http://example.com/whatever.html#//var1=blablabla&var2=bar&gghh=edere

Notice: it changed the value of var2 properly. But it did add an extra slash at the beginning.

The problem is that it won't add the parameter, if no such key previously existed. If you want to do this, then I'd recommend something like the following:

function _parseQueryString( queryString ) {
    var pairs = queryString.split('&');
    var params = {};
    for(var i=0; i<pairs.length; ++i ) {
       pairs[i] = pairs[i].trim();
       if( pairs[i] == '' ) continue;

        var parts = pairs[i].split('=');
        if( parts.length == 0 ) continue;
        var name = parts.shift();
        var value = '';
        while(parts.length) {
            value += parts.shift();
        }
        params[name]=value;
    }
    return params;
}

function _buildQueryString( params ) {
    var queryString = "";
    for( var i in params ) {
        if( queryString.length > 0 ) queryString += "&";
        queryString += (i + '=' +params[i]);
    }
    return queryString;
}

function set_part(queryString, key, value) {
    /* first remove the leading '#/', if any */
    if( queryString.indexOf('#')==0 ) {
        queryString = queryString.substring(1);
    }
    if( queryString.indexOf('/')==0 ) {
        queryString = queryString.substring(1);
    }

   /* now parse the hash (in queryString format) into 
      an object containing all the parts */
   var params = _parseQueryString(queryString);

   /* finally, just set the value in the params object, 
      and rebuild the query string, adding 
      in the slash again 
   */
   params[key]=value;
   window.location.hash = '/' + _buildQueryString(params);
}

Now, if the url of your page is

http://example.com/whatever.html#/var1=blablabla&var2=var2text&gghh=edere

and you call it like this:

alert( 'hash before: '+window.location.hash );

set_part( window.location.hash, 'var2', 'bar');
set_part( window.location.hash, 'foo', 'foovalue');

alert( 'hash after: '+window.location.hash );

then you can see that both forms work correctly.

You can see this example in action at jsfiddle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文