简体   繁体   English

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

[英]how to exchange variables between two HTML pages?

I have two HTML pages, example1.html and example2.html . 我有两个HTML页面, example1.htmlexample2.html

How do I pass variables from example1.html to example2.html using the query string, and retrieve that variable in example2.html without using any serverside code? 如何使用查询字符串将变量从example1.html传递到example2.html ,并在不使用任何服务器端代码的情况下在example2.html检索该变量?

In example1.html: 在example1.html中:

<a href='example2.html?myVar1=42'>a link</a>
<a href='example2.html?myVar1=43'>another link</a>

or generate the links with Javascript as desired. 或根据需要使用Javascript生成链接。 Just make sure the ?varName=value gets onto the end of example2.html somehow. 只要确保?varName = value以某种方式进入example2.html的末尾。

Then, in example2.html, you use Javascript to parse the query string that example2 came with. 然后,在example2.html中,您使用Javascript来解析example2附带的查询字符串。

To do this, you could try Querystring. 为此,您可以尝试使用Querystring。

// Adapted from examples on the Querystring homepage.
var qs = new Querystring();
var v1 = qs.get("myVar1");

Alternatively, parent.document.URL contains the complete URI for the page you're on. 或者,parent.document.URL包含您所在页面的完整URI。 You could extract that: 你可以提取:

parent.document.URL.substring(parent.document.URL.indexOf('?'), parent.document.URL.length);

and parse it manually to pull the variables you encoded into the URI. 并手动解析它以将您编码的变量拉入URI。

EDIT: Forgot the 'new' part of 'new Querystring()'. 编辑:忘记'新Querystring()'的'新'部分。 Oops... 糟糕!

HTML / HTTP is stateless, this means that, what you did / saw on the previous page, is completely disconnected with the current page.Question is How to pass variable between two pages in front end. HTML / HTTP是无状态的,这意味着,您在上一页上所做/所看到的内容与当前页面完全断开。问题是如何在前端的两个页面之间传递变量。 (As HTTP is stateless, every time you load the page it will use the initial values of whatever you set in JavaScript. You can't set a global variable in JS and simply make that value stay after loading the page again. (由于HTTP是无状态的,每次加载页面时,它都将使用您在JavaScript中设置的任何内容的初始值。您无法在JS中设置全局变量,只需在再次加载页面后保持该值。

There are a couple of ways you could store the value in another place so that you can initialize it on load using JavaScript) 有几种方法可以将值存储在另一个地方,以便您可以使用JavaScript在加载时初始化它)

1) - Simple using Front End Storages, which equips any Browser (Cookie, Session Storage, Local Storage - which for security reasons available throughout one domain -> it means you can save data on this storage only for one domain, another domain can't access this data ) and put value in one page and get value in others. 1) - 简单地使用前端存储,它配备任何浏览器(Cookie,会话存储,本地存储 - 出于安全原因在整个域中可用 - >这意味着您可以仅在一个域中保存此存储上的数据,另一个域可以' t访问此数据)并将值放在一个页面中并在其他页面中获取值。 Considering that: 考虑到:

Cookie saves data until what time you have determined, Cookie将数据保存到您确定的时间,

Session Storage saves data until the browser default tab closed. 会话存储保存数据,直到浏览器默认选项卡关闭。

Local Storage saves data until Browser completely closed and shares data between tabs (pages) It stores data with no expiration date, and gets cleared only through JavaScript, or clearing the Browser Cache / Locally Stored Data - unlike cookie expiry. 本地存储保存数据,直到浏览器完全关闭并在选项卡(页面)之间共享数据它存储没有过期日期的数据,并且仅通过JavaScript清除,或清除浏览器缓存/本地存储的数据 - 与cookie过期不同。

2) – Add attributes to the element when it generated via Ajax render function 2) - 通过Ajax渲染函数生成属性时添加属性

<a href='example2.html?action=getAll&element=product&id=1'>a link</a>
<a href='example2.html?action=getAll&element=product&id=2'>another link</a>

-> and after click this element construct “ URL / ? - >点击此元素后构建“URL /? action=getAll & element=product & id=1 “ and in second page which will be gone action you can parse this URL and call appropriate Ajax with appropriate parameters. action = getAll&element = product&id = 1“并且在第二页中将执行操作,您可以解析此URL并使用适当的参数调用适当的Ajax。

您可以使用cookie和Querystring来交换值。

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

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