Javascript 在页面之间传递javascript变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11581543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:24:00  来源:igfitidea点击:

Passing javascript variables between pages

javascripthtmlforms

提问by Rachel Rine

Possible Duplicate:
Persist javascript variables across pages?
how to exchange variables between two html pages

可能的重复:
跨页面保留 javascript 变量?
如何在两个html页面之间交换变量

I am working on creating a "quiz" that allows users to answer questions, then passes their answer and current score to the next page for the next question. I used this article and was able to get the user's answer to pass through to the next window:

我正在创建一个“测验”,允许用户回答问题,然后将他们的答案和当前分数传递到下一个问题的下一页。我使用了这篇文章并且能够获得用户的答案以传递到下一个窗口:

http://www.htmlgoodies.com/beyond/javascript/article.php/3471111/A-Quick-Tutorial-on-JavaScript-Variable-Passing.htm

http://www.htmlgoodies.com/beyond/javascript/article.php/3471111/A-Quick-Tutorial-on-JavaScript-Variable-Passing.htm

The issue is that this passes the information through a form. Once I receive the score on the second page, I need to increment the score if the answer was correct. I can't figure out how to pass a javascript variable with an html form.

问题是这通过表单传递信息。一旦我在第二页上收到分数,如果答案正确,我需要增加分数。我不知道如何使用 html 表单传递 javascript 变量。

I feel like this should be a relatively easy fix, I'm just stumped.

我觉得这应该是一个相对容易的修复,我只是被难住了。

Any thoughts or advice would be much appreciated! xoxo

任何想法或建议将不胜感激!嗖嗖嗖嗖

回答by Alnitak

There are two obvious ways to maintain state in the browser without requiring that the server remember it between pages:

有两种明显的方法可以在浏览器中维护状态,而无需服务器在页面之间记住它:

  1. Cookies

  2. localStorage

  1. 饼干

  2. localStorage

The latter is trivial to implement, but is only available in HTML5.

后者实现起来很简单,但仅在 HTML5 中可用。

Note that neither is intended to be secure- a determined page hacker could set either to whatever value they wish.

请注意,两者都不是安全的- 确定的页面黑客可以将其设置为他们希望的任何值。

回答by Trinh Hoang Nhu

You can submit a form using get method <form method="get"then use javascript to parse params in url. Referencehere:

您可以使用 get 方法提交表单,<form method="get"然后使用 javascript 解析 url 中的参数。参考这里: