Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取iframe中的localstorage项_Javascript_Jquery_Html_Local Storage - Fatal编程技术网

Javascript 获取iframe中的localstorage项

Javascript 获取iframe中的localstorage项,javascript,jquery,html,local-storage,Javascript,Jquery,Html,Local Storage,我有一个文件index.html,其中有一个iframe集,iframe源指向另一个域。我正在index.html中设置localstorage,并尝试在iframe source中获取值。(sample.html) 文件1 index.html //js文件 设置本地存储 Iframe src=“55.10.45.045/sample.html” 文件2 sample.html //js文件 获取localstorage项//返回null 像这样使用 index.html <!DOCTY

我有一个文件
index.html
,其中有一个iframe集,iframe源指向另一个域。我正在
index.html
中设置localstorage,并尝试在iframe source中获取值。(
sample.html

文件1
index.html
//js文件
设置本地存储
Iframe src=“55.10.45.045/sample.html”
文件2
sample.html
//js文件
获取localstorage项//返回null
像这样使用

index.html

<!DOCTYPE html>
<html>
<body>

<div id="result"></div>
<a href="sample.html">click</a>
<script>
// Check browser support
if (typeof(Storage) !== "undefined") {
    // Store
    localStorage.setItem("link", "http://www.w3schools.com/");
    // Retrieve
    document.getElementById("result").innerHTML = localStorage.getItem("link");
} else {
    document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
}
</script>

</body>
</html>

//检查浏览器支持
if(类型(存储)!=“未定义”){
//贮藏
setItem(“链接”http://www.w3schools.com/");
//取回
document.getElementById(“结果”).innerHTML=localStorage.getItem(“链接”);
}否则{
document.getElementById(“结果”).innerHTML=“对不起,您的浏览器不支持Web存储…”;
}
sample.html

<!DOCTYPE html>
<html>
<body>

<div id="result"></div>
<script>
// Check browser support
if (typeof(Storage) !== "undefined") {

    document.getElementById("result").innerHTML = "<iframe src='"+localStorage.getItem("link")+"' width='100%' height='350px' />";
} else {
    document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
}
</script>

</body>
</html>

//检查浏览器支持
if(类型(存储)!=“未定义”){
document.getElementById(“结果”).innerHTML=“”;
}否则{
document.getElementById(“结果”).innerHTML=“对不起,您的浏览器不支持Web存储…”;
}
像这样使用

index.html

<!DOCTYPE html>
<html>
<body>

<div id="result"></div>
<a href="sample.html">click</a>
<script>
// Check browser support
if (typeof(Storage) !== "undefined") {
    // Store
    localStorage.setItem("link", "http://www.w3schools.com/");
    // Retrieve
    document.getElementById("result").innerHTML = localStorage.getItem("link");
} else {
    document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
}
</script>

</body>
</html>

//检查浏览器支持
if(类型(存储)!=“未定义”){
//贮藏
setItem(“链接”http://www.w3schools.com/");
//取回
document.getElementById(“结果”).innerHTML=localStorage.getItem(“链接”);
}否则{
document.getElementById(“结果”).innerHTML=“对不起,您的浏览器不支持Web存储…”;
}
sample.html

<!DOCTYPE html>
<html>
<body>

<div id="result"></div>
<script>
// Check browser support
if (typeof(Storage) !== "undefined") {

    document.getElementById("result").innerHTML = "<iframe src='"+localStorage.getItem("link")+"' width='100%' height='350px' />";
} else {
    document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
}
</script>

</body>
</html>

//检查浏览器支持
if(类型(存储)!=“未定义”){
document.getElementById(“结果”).innerHTML=“”;
}否则{
document.getElementById(“结果”).innerHTML=“对不起,您的浏览器不支持Web存储…”;
}

您想使用这样的东西

发送信息:

  window.onload = function() {
      var win = document.getElementById('iFrameId').contentWindow;

      win.postMessage(JSON.stringify({

          key: 'dataKey',
          data: dataPassing

      }), "*");

  };
window.onmessage = function(e) {

     var payload = JSON.parse(e.data);
     localStorage.setItem(payload.key, payload.data);

};
接收信息:

  window.onload = function() {
      var win = document.getElementById('iFrameId').contentWindow;

      win.postMessage(JSON.stringify({

          key: 'dataKey',
          data: dataPassing

      }), "*");

  };
window.onmessage = function(e) {

     var payload = JSON.parse(e.data);
     localStorage.setItem(payload.key, payload.data);

};

这将把一个JSON对象传递到iFrame中,然后框架中的脚本将获取它并将其推送到本地存储中

你想用这样的东西

发送信息:

  window.onload = function() {
      var win = document.getElementById('iFrameId').contentWindow;

      win.postMessage(JSON.stringify({

          key: 'dataKey',
          data: dataPassing

      }), "*");

  };
window.onmessage = function(e) {

     var payload = JSON.parse(e.data);
     localStorage.setItem(payload.key, payload.data);

};
接收信息:

  window.onload = function() {
      var win = document.getElementById('iFrameId').contentWindow;

      win.postMessage(JSON.stringify({

          key: 'dataKey',
          data: dataPassing

      }), "*");

  };
window.onmessage = function(e) {

     var payload = JSON.parse(e.data);
     localStorage.setItem(payload.key, payload.data);

};

这将把一个JSON对象传递到iFrame中,然后框架中的脚本将获取它并将其推送到本地存储中

我认为那是不可能的。我们说的是在一个域中设置本地存储,然后在另一个域中获取,据我所知,这是不可能的。设置和获取由浏览器在本地完成。无论位置如何,也不管是在iframe中还是在iframe外。但问题是上面的代码无论如何都不是有效的HTML。所以…@hakuna matata:我恳请你继续读下去。那么,带着一张照片回来。我认为那是不可能的。我们说的是在一个域中设置本地存储,然后在另一个域中获取,据我所知,这是不可能的。设置和获取由浏览器在本地完成。无论位置如何,也不管是在iframe中还是在iframe外。但问题是上面的代码无论如何都不是有效的HTML。所以…@hakuna matata:我恳请你继续读下去。然后,带着一张支票回来。