How to change the current URL in javascript?

后端 未结 4 905
名媛妹妹
名媛妹妹 2020-12-28 13:30

On my website:

http://mywebsite.com/1.html

I want to use the

window.location.pathname

to get the last pa

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-28 13:49

    This is more robust:

    mi = location.href.split(/(\d+)/);
    no = mi.length - 2;
    os = mi[no];
    mi[no]++;
    if ((mi[no] + '').length < os.length) mi[no] = os.match(/0+/) + mi[no];
    location.href = mi.join('');
    

    When the URL has multiple numbers, it will change the last one:

    http://mywebsite.com/8815/1.html
    

    It supports numbers with leading zeros:

    http://mywebsite.com/0001.html
    

    Example

提交回复
热议问题