簡體   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