我有2个html文件,在我的第一个文件中我声明了一个变量,我想在我的第二个文件中使用相同的变量......
我的第一个文件代码是
var call = Expression.Call(
closedAnyMethod,
propertyExp,
lambdaAny);
我想使用" ids"我的第二个文件变量... 感谢;
答案 0 :(得分:0)
如果HTML文档具有相同的来源,您可以使用postMessage
,MessageChannel
,SharedWorker
或storage
事件在不同的浏览上下文之间进行通信,请参阅
您可以使用localStorage
和storage
事件来使用相同的对象变量,或者在不同的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>