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

标签 javascript html query-string href

我有两个 HTML 页面,example1.htmlexample2.html

如何使用查询字符串将变量从 example1.html 传递到 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 附带的查询字符串。

为此,您可以尝试使用 Querystring。

// 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()”的“new”部分。糟糕...

关于javascript - 如何在两个 HTML 页面之间交换变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3724106/

相关文章:

javascript - 引导日期选择器今天默认使用 Angular 和 jquery 在 h4 中显示

javascript - 如何使用 webapp 生成器 gulp 构建非缩小的生产代码?

html - 在 HTML5 中使用章节/文章语义元素的最佳实践

javascript - GIF 的尺寸会影响网页的性能吗?

php - Mysql在缺少html图像时创建双插入

javascript - 如何通过 JavaScript 获取媒体查询定义的当前 CSS 值?

query-string - 识别模式 : what system is malforming my query string parameters?

javascript - Jquery 选择第一个字母?

asp.net - 如何从 asp.net 中具有 UNICODE 值的 URL 获取正确的 QueryString 值?

javascript - 可以检测到在控制台浏览器中运行的 javascript 代码吗