2016-06-20 44 views
0

ファイルがindex.htmlでiframeが1セットあり、iframeソースが別のドメインを指しています。私はindex.htmlでのlocalStorageを設定するとiframeソース内の値を取得しようとしています。(sample.htmliframeにlocalstorageアイテムを取得

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 
+1

私はそれが可能ではないと思います。あるドメインでlocalstorageを設定し、それが私の知る限りではない別のドメインで取得することについて話していますか? – Akki619

+1

設定と取得はブラウザによってローカルで行われます。場所やiframeの有無に関わらず。しかし、上のコードは、有効なHTMLではないことです。だから... –

+0

@hakuna matata:[iframeタグ](http://www.w3schools.com/tags/tag_iframe.asp)と[localStorage](http:// www。 w3schools.com/html/html5_webstorage.asp)。 **その後**、[最小で完全で検証可能なコード](http://stackoverflow.com/help/mcve)に戻ってください。 –

答えて

0

このような使用...

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> 

を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> 
0

このようなものを使用します。

情報を送信:

情報を受信
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); 

}; 

これは、フレーム内のスクリプトは、それを取ると、ローカルストレージに押し込みます、iFrameのにJSONオブジェクトを渡します。

関連する問題