How to change the current URL in javascript?

后端 未结 4 916
名媛妹妹
名媛妹妹 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:57

    What you're doing is appending a "1" (the string) to your URL. If you want page 1.html link to page 2.html you need to take the 1 out of the string, add one to it, then reassemble the string.

    Why not do something like this:

    var url = 'http://mywebsite.com/1.html';
    var pageNum = parseInt( url.split("/").pop(),10 );
    var nextPage = 'http://mywebsite.com/'+(pageNum+1)+'.html';
    

    nextPage will contain the url http://mywebsite.com/2.html in this case. Should be easy to put in a function if needed.

提交回复
热议问题