如何在两个HTML页面之间交换变量? [英] how to exchange variables between two HTML pages?
问题描述
我有两个HTML页面, example1.html
和 example2.html
。
如何使用查询将变量从 example1.html
传递给 example2.html
字符串,然后在 example2.html
中检索该变量,而不使用任何服务器端代码?
在example1.html中:
< a href ='example2.html?myVar1 = 42'> a链路< / A>
< a href ='example2.html?myVar1 = 43'>另一个连结< / a>
或者根据需要使用Javascript生成链接。然后,在example2.html中,使用Javascript解析查询字符串example2随同。
要做到这一点,你可以试试Querystring。
//根据Querystring主页上的示例进行调整。
var qs = new Querystring();
var v1 = qs.get(myVar1);
或者,parent.document.URL包含您所在页面的完整URI。你可以提取:
$ b $ pre $ parent.document.URL.substring(parent.document.URL.indexOf('?'), parent.document.URL.length);
并手动解析它以将您编码的变量拉入URI中。
编辑:忘记'新的Querystring()'的'新'部分。糟糕...
I have two HTML pages, example1.html
and example2.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?
In 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. Just make sure the ?varName=value gets onto the end of example2.html somehow.
Then, in example2.html, you use Javascript to parse the query string that example2 came with.
To do this, you could try 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. 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.
EDIT: Forgot the 'new' part of 'new Querystring()'. Oops...
这篇关于如何在两个HTML页面之间交换变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!