admin管理员组

文章数量:1289986

I have a file index.html with one iframe set and iframe source points to another domain. I am setting localstorage in index.html and trying to get the value in iframe source.(sample.html)

File 1
index.html
    <html>
    <head>
    //js file 
    </head>
    <body>
    setting localstorage
    Iframe src="55.10.45.045/sample.html"
    </body>
    </html>

file 2
sample.html
       <html>
        <head>
        //js file 
        </head>
        <body>
        getting localstorage Item //Returns null
        </body>
        </html

I have a file index.html with one iframe set and iframe source points to another domain. I am setting localstorage in index.html and trying to get the value in iframe source.(sample.html)

File 1
index.html
    <html>
    <head>
    //js file 
    </head>
    <body>
    setting localstorage
    Iframe src="55.10.45.045/sample.html"
    </body>
    </html>

file 2
sample.html
       <html>
        <head>
        //js file 
        </head>
        <body>
        getting localstorage Item //Returns null
        </body>
        </html
Share Improve this question edited Jun 20, 2016 at 8:32 Angelos Chalaris 6,7078 gold badges53 silver badges80 bronze badges asked Jun 20, 2016 at 8:12 hakuna matatahakuna matata 291 gold badge1 silver badge5 bronze badges 3
  • 1 I don't think that's possible. Are we talking about setting up localstorage in one domain and getting that in another domain that's not possbile as far as I know. – Akki619 Commented Jun 20, 2016 at 8:17
  • 1 The set and get is done by the browser, locally. No matter about the location or if in or out an iframe. But the thing is that code above is NOT valid HTML anyway. So... – Louys Patrice Bessette Commented Jun 20, 2016 at 8:20
  • @hakuna matata: I kindly suggest you to read more on iframe tag and on localStorage. Then, e back with a minimal, plete and verifiable code. – Louys Patrice Bessette Commented Jun 20, 2016 at 8:28
Add a ment  | 

2 Answers 2

Reset to default 6

You want to use something like this.

Sending information:

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

      win.postMessage(JSON.stringify({

          key: 'dataKey',
          data: dataPassing

      }), "*");

  };

Receiving information:

window.onmessage = function(e) {

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

};

This will pass a JSON object into the iFrame, then the script in the frame will take it and push it into local storage.

Use like this...

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./");
    // Retrieve
    document.getElementById("result").innerHTML = localStorage.getItem("link");
} else {
    document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
}
</script>

</body>
</html>

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>

本文标签: javascriptget localstorage item in a iframeStack Overflow