如何使用javascript将一个文件输入id调用到另一个文件中

时间:2017-09-24 17:56:06

标签: javascript html

我有2个html文件,在我的第一个文件中我声明了一个变量,我想在我的第二个文件中使用相同的变量......

我的第一个文件代码是

var call = Expression.Call(
        closedAnyMethod,
        propertyExp,
        lambdaAny);

我想使用" ids"我的第二个文件变量... 感谢;

1 个答案:

答案 0 :(得分:0)

如果HTML文档具有相同的来源,您可以使用postMessageMessageChannelSharedWorkerstorage事件在不同的浏览上下文之间进行通信,请参阅

您可以使用localStoragestorage事件来使用相同的对象变量,或者在不同的HTML localStorage a处定义设置为document值的局部变量拥有相同的域名。

<!DOCTYPE html>
<html>
<head>
  <title>index</title>
</head>

<body>
  <a href="otherPage.html" target="_blank">otherPage.html</a>
  <h1>set id</h1>
  <script>
    let id;
    let h1 = document.querySelector("h1");
    h1.onclick = e => {
      id = `${Math.E * Math.PI * Math.random()}`;
      localStorage.setItem("id", id);
      console.log(`id: ${id} at ${e.type}`);
    }
  </script>
</body>

</html>

<!DOCTYPE html>
<html>
<head>
  <title>other page</title>
  <script>
    let id = localStorage.getItem("id");
    console.log(`id: ${id} at ${document.title}`);
    onstorage = e => {
      console.log(`id: ${localStorage.getItem("id")} at ${e.type}`);
      id = localStorage.getItem("id");
      console.log(id);
    }
  </script>
</head>

<body>
  <h1>otherPage.html</h1>
</body>

</html>

plnkr https://plnkr.co/edit/m4RIdwgIl74Dk6YmGAgI?p=preview