【问题标题】:pass variables through html files通过 html 文件传递​​变量
【发布时间】:2014-09-02 08:56:17
【问题描述】:

我想知道是否有任何方法可以通过.html 文件传递​​vars。

例如在我的index.html<a> 中点击设置:

var value = 'some text here';

并链接到other.html。 我的other.html 中第一个执行的脚本是:

console.log(value);

HTML 文件可以通过这种方式传递变量吗?

【问题讨论】:

  • 使用 URL 或 cookie。如果是 URL,则访问 location.searchlocation.hash
  • 尝试将 other.html 打开为“other.html#yourVariableValue”,然后在 other.html 中 var value = window.location.hash;
  • @AbdulJabbar 谢谢,效果很好!

标签: javascript html var


【解决方案1】:

您可以将任何数据从一个页面发送到另一个页面,方法是将其作为 queryString 传递,例如 host/other.html?value=something

在第一页使用 javascript 或 jquery 将您的数据附加到 URL 中,并在第二页使用 javascript 或 jquery 检索它

【讨论】:

  • 在您的 javascript 函数中包含以下函数 getParameterByName(name) { name = name.replace(/[[]/, "\[").replace(/[]]/, "\] "); var regex = new RegExp("[\\?&]" + name + "=([^]*)"), results = regex.exec(location.search);返回结果 === 空? "" : decodeURIComponent(results[1].replace(/\+/g, " "));现在您可以使用以下代码行获取您传递的值 var value= getParameterByName('value');
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-08
  • 2016-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多