简体   繁体   English

在HTML页面之间维护Javascript变量以发送到数据库?

[英]Maintaining a Javascript variable between HTML pages to send to database?

I'm doing an experiment for university that involves timing how long participants take to read a simple webpage. 我正在大学进行一项实验,其中涉及确定参与者阅读简单网页所花的时间。 Because of the nature of the experiment, I can't tell them I'm timing them before they visit the page, so on the following page I need to tell them that they were being timed and then offer an option to opt out if they object to their data being passed on to a database using a Python CGI script. 由于实验的性质,我无法告诉他们在他们访问该页面之前对他们进行计时,因此在下一页上,我需要告诉他们他们正在被计时,然后提供一个选项来选择是否退出他们反对使用Python CGI脚本将其数据传递到数据库。

At the top of the first page I have this: 在第一页的顶部,我有这个:

<script type="text/javascript" src="timing.js">
</script>

And at the bottom I have a link to take them to the next page, but also to stop the timer: 在底部,我有一个链接,可以将它们带到下一页,还可以停止计时器:

Click here when you are ready to proceed.
<button onclick="window.location = 'timer2.html'; stopTime()">Click Here</button>

...and in the .js file, I have the following: ...并且在.js文件中,我具有以下内容:

var sec = 0;

function takeTime()
{
    setInterval(function(){sec += 1},1000);
}

myTime = takeTime();

function stopTime()
{
clearInterval(myTime);
}

function showTime()
{
alert("Time is " + sec + " seconds.");
}

When I click the link to go the second page, the "sec" variable just resets to 0. If I take the declaration out of the .js file and put it in a separate script tag before the one that refers to the .js file in the first HTML page, the second HTML page doesn't recognise the variable "sec" at all. 当我单击链接转到第二页时,“ sec”变量仅重置为0。如果我从.js文件中取出声明,并将其放在引用.js文件的变量之前的单独的脚本标记中。在第一个HTML页面中,第二个HTML页面根本无法识别变量“ sec”。 How can I store the time so that I can then either send it to a database or get rid of it from the second HTML page? 如何存储时间,以便可以将其发送到数据库或从第二个HTML页面中删除它?

One way is to use the query string. 一种方法是使用查询字符串。

<button onclick="window.location = 'timer2.html?time=' + sec">Click Here</button>

They will be sent to eg timer2.html?time=252 if it took 252 seconds. 如果花费了252秒,它们将被发送到例如timer2.html?time=252 Then use How can I get query string values in JavaScript? 然后使用如何获取JavaScript中的查询字符串值? to get this value on timer2.html. 在timer2.html上获取此值。

Javascript variables do not maintain their state from one page load to the next, so you will need another way to store that information. 从一个页面加载到下一个页面,JavaScript变量不会保持其状态,因此您将需要另一种方式来存储该信息。 Another method besides passing it through the query string is HTML5 Local Storage. 除了通过查询字符串传递之外,另一种方法是HTML5 Local Storage。 Using Local Storage, you can store values in the browser that are retrievable from one page load to the next, within the same domain. 使用本地存储,您可以在浏览器中存储可在同一域内从一页加载到下一页加载检索的值。

For example, your first page: 例如,您的第一页:

var sec = 0;
localStorage.time = 0;

function takeTime()
{
    setInterval(function(){sec += 1},1000);
}

myTime = takeTime();

function stopTime()
{
    clearInterval(myTime);
    localStorage.time = sec;
}

function showTime()
{
alert("Time is " + sec + " seconds.");
}

And somewhere in timer2.html: 在timer2.html中的某处:

var sec = localStorage.time;

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

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