我的项目是有几页的调查。我想将多个页面的答案上传到同一个 uniqueID 中。现在我可以从页面 js 文件中获取 uniqueID,但是如何将此 uniqueID 传输到其他 js 文件,以便我可以将数据上传到相同的 uniqueID。
最佳答案
问题可以重新表述为如何通过不同的页面和 js 文件访问变量值。
- 我更喜欢的方法 - 使用大量数据 - 是加载“全局脚本”的用户不可见的基页。该基页加载和卸载所有其他页面(包括 js 和 css 文件)。由于您的情况中的变量 firebaseID 是全局定义的,因此它可以工作。
- 如果它不是敏感数据,您也可以将其放入本地存储(最好是加密的)。如果用户想要暂停调查并稍后完成调查,这也会有所帮助。 (移动设备上有 5MB,桌面设备上有 10MB)。
有关解决方案一的更多详细信息:
最简单的部分是将 HTML 插入到其他 HTML 中。我使用服务器生成的 View ,以便根据需要添加和删除基本页面的 HTML。
更困难的部分是页面( View )特定的 js 加载。
在我的用例中,用户可以通过导航跳转,直到最终提交并完成。如果你只允许顺序向前移动而不向后移动,那就更容易了。我实际上模仿了一种安静的行为。基本脚本中的一些构建 block 包括
// Some global vars
var statMsg = "";
var navTargetInitial= "app";
var navTarget;
var htmlDir = "app"; // directory of the html templates
var scriptDir = "js"; // directory of the user scripts
var routes = [
{
navTarget: "main",
loadScript: true,
navTitle: "Survey page 1",
navMenutext: "Page 1"
}, {
navTarget: "app",
loadScript: true,
navTitle: "Survey page 2",
navMenutext: "Page 2"
},
....
];
// Then docReady beside other stuff
function docReady() {
// other stuff
nav2Page(navTarget);
}
// here we handover data, put relevant things to sessionStorage
window.onbeforeunload = function (e) {
// Do stuff
};
// here we cleanup
window.onunload = function () {
// cleanup
};
关键部分是在 html 进入 DOM 后加载新的 js,因此重要部分是加载事件 DOM 上的事件处理程序
document.addEventListener("DOMContentLoaded",
loadJScript(navScript, function(response, status, xhr ){// error handling }
剩下的就是ajax、错误处理、对页面历史记录中后退/前进的点击的 react 以及相应的 react 。 希望这有帮助
关于javascript - 如何从firebase跨多个页面获取唯一ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60963233/