繁体   English   中英

如何制作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

我需要将500加1才能使它成为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
})()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM