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
2 Answers
Reset to default 6You 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
版权声明:本文标题:javascript - get localstorage item in a iframe - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741456859a2379806.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论