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

2023-11-25

我有两个 HTML 页面,example1.html and example2.html.

我如何传递变量example1.html to example2.html使用查询字符串,并检索该变量example2.html不使用任何服务器端代码?


在 example1.html 中:

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

或者根据需要使用 Javascript 生成链接。只需确保 ?varName=value 以某种方式到达 example2.html 的末尾即可。

然后,在 example2.html 中,您使用 Javascript 来解析 example2 附带的查询字符串。

为此,您可以尝试查询字符串。

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

或者,parent.document.URL 包含您所在页面的完整 URI。你可以提取:

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

并手动解析它以将您编码到 URI 中的变量提取出来。

编辑:忘记了“new Querystring()”的“新”部分。哎呀...

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在两个HTML页面之间交换变量? 的相关文章

随机推荐