在页面之间传递JavaScript变量 [英] Passing javascript variables between pages

查看:87
本文介绍了在页面之间传递JavaScript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

在页面上持久化JavaScript变量?

如何在两个html页面之间交换变量


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

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



问题是,这通过表单传递信息。一旦我在第二页上收到分数,如果答案正确,我需要增加分数。我无法弄清楚如何传递一个HTML格式的JavaScript变量。



我觉得这应该是一个相对简单的修复,我只是难住。 / p>

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

解决方案

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


  1. Cookies

  2. localStorage


后者实现起来微不足道,但仅适用于HTML5。



请注意,旨在成为 secure - 一个确定的页面黑客可以设置他们希望的任何值。


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

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

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.

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

Any thoughts or advice would be much appreciated! xoxo

解决方案

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

  1. Cookies

  2. localStorage

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

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

这篇关于在页面之间传递JavaScript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆