我有一个功能可以在浏览器的新选项卡中打开 PDF。用户可能想导航到与打开的页面不同的页面,因此页面也可以通过函数更新:
openpdf: function(url, page, pageCount){
try {
console.log('the page count is ' + pageCount);
var page = new Number(page) + new Number(pageCount);
if (mobileAndTabletcheck()){
console.log('mobile detected');
return window.open(url + '#page' + page, 'somename');
}
console.log('no mobile detected')
return window.open(url + '#page=' + page, 'somename');
}
catch(e){
console.log(e)
}
},
我遇到的问题是,当用户单击以导航到 pdf 的新页面时,URL 会更新为正确的页码,但查看者停留在最初打开的页面上。
有谁知道如何在更新URL时使当前页面更新?
最佳答案
您可以使用本地存储作为跟踪打开文档的位置。我相信 pdf 查看器和主页保持在同一个域中。所以你可以尝试这样的事情。
openpdf: function(url, page, pageCount){
try {
console.log('the page count is ' + pageCount);
var page = new Number(page) + new Number(pageCount);
if (mobileAndTabletcheck()){
console.log('mobile detected');
return window.open(url + '#page' + page, 'somename');
}
console.log('no mobile detected')
return window.open(url + '#page=' + page, 'somename');
}
catch(e){
console.log(e)
}
var openedLinks = JSON.parse(localStorage.getItem("links"));
openedLinks.push("Your URL here");
localStorage.setItem("links", JSON.stringify(names));
}
在 pdf 查看器页面上,只需更新本地存储即可。
onpageChanged : function(){
var openedLinks = JSON.parse(localStorage.getItem("links"));
var index = items.indexOf("Your current Url");
if (index !== -1) {
openedLinks[index] = "Your updated URL";
}
localStorage.setItem("links", JSON.stringify(names));
}
最后在主页上,使用类似计时器的东西来跟踪打开的链接。
setInterval(function(){
var openedLinks = JSON.parse(localStorage.getItem("links"));
$("#someDiv").html(openedLinks.join(","));
},1000);
希望这对你有用。代码未经测试,可能有错误。
关于javascript - 更新在新浏览器选项卡中打开的 pdf 的当前页码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46031928/