如何制作 JavaScript 小书签以添加到 URL 的一部分?

How do I make a JavaScript bookmarklet to add to part of the URL?u

我正在尝试制作一个 JavaScript 小书签以转到我的数学书的下一页。 URL 类似于 website.com/book_year_pagenumber.pdf,因此第 500 页将是 website.com/book_year_0500.pdf

我需要将 1 添加到 500 以使其成为 website.com/book_year_0501.pdf,但它必须在任何页面上工作,例如从第 200 页到第 201 页或第 573 页到第 574 页。有谁知道如何做到这一点?

编辑:也许这会有所帮助。我想让它变成“http://my.hrw.com/math06_07/student/pdf/english/alg2/alg2_07_0500.pdf” 进入 “http://my.hrw.com/math06_07/student/pdf/english/alg2/alg2_07_0501.pdf

javascript:(function(){var url=location.href.split('book_year_');var currentPage=+(url[1].split('.')[0])+1;while(currentPage.toString().length<4){ currentPage='0'+currentPage;}location.href=url[0]+('book_year_')+currentPage+'.pdf';})() 不行吗?

稍微浏览一下:

// declare that this bookmarklet is a js script
javascript:(function(){
  // parse the current url
  var url = location.href.split('book_year_');
  // get the page number, transform it to a Number and increment it
  var currentPage = +(url[1].split('.')[0])+1;
  // add the leading zeroes
  while(currentPage.toString().length<4){ currentPage = '0' + currentPage;}
  // set the new url      
  location.href= url[0]+('book_year_')+currentPage+'.pdf';
// call the function
})()