将唯一的本地存储与域上的唯一网页绑定的最佳方式
我有一个 JavaScript 引擎,可以从典型的网页创建“杂志”。几个网页会有单独的“问题”,所有页面都使用相同的 javascript 引擎来驱动格式和行为。
该引擎使用本地存储来记住用户上次阅读该期杂志时所在的该期的“页面”。
localStorage.setItem('currentPage',JSON.stringify(currentPage));
var currentPage = JSON.parse(localStorage.getItem('currentPage'));
var remover = $('.article-wrapper:eq(' + currentPage + ')');
我的问题是,如果我访问一本名为“秋季”的杂志的第 5 页,关闭它并打开一本名为“春季”的杂志,那么我将自动从春季的第 5 页开始。
有没有办法可以为该问题的 html 中的每个问题设置唯一的“currentPage”变量名称,使用引擎的 javascript 读取它,然后将其保存到用户正在使用的计算机/设备的本地存储中?
您可以通过访问这两个站点来测试该问题。两者都打开。转到一个站点的第五页,然后刷新另一个站点。它将更改为其他页码。
I have a javascript engine that creates a 'magazine' out of a typical web page. Several web pages would have separate 'issues', all using the same javascript engine that drives the formatting and behaviour.
The engine uses local storage to remember which 'page' of the issue the user was on when they last read the issue of the magazine.
localStorage.setItem('currentPage',JSON.stringify(currentPage));
var currentPage = JSON.parse(localStorage.getItem('currentPage'));
var remover = $('.article-wrapper:eq(' + currentPage + ')');
my problem is, if I visit page 5 of a magazine titled 'autumn', close it and open a magazine titled 'spring', then I will start on page 5 of spring automatically.
Is there a way I can set a unique 'currentPage' variable name for each issue in the html for that issue, read it with the javascript for the engine and then have it save to local storage for the computer/device the user is using?
You can test the problem by visiting these two sites. Open both. Go to page five of one site and then refresh the other. It will change to the others page number.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要将
currentPage
存储为简单的整数,而是存储一个跟踪当前页面的 url 和该 url 的页面的对象。例如:
如果您不喜欢将完整的 URL 作为键,那么您可以使用
indexOf
来查找页面名称。有关更多信息,请参阅此 StackOverflow 答案。Instead of storing the
currentPage
as a simple integer, store an object that keeps track of both the current page's url and the page for that url.For example:
If you do not like having the full URL as the key, then you could use
indexOf
to find the pages name. See this StackOverflow answer for more.