[英]Get value from DOM in iframe (not parent page)
我知道這個主題可能有點混亂,但是在我的搜索中,我發現很多關於在iframe中從父頁面獲取價值的問題。 這不是我想要達到的目標。
我有2個html頁面和一個js文件。 這是它們的外觀:
external.html:
...
<body>
<iframe src="./inner.html"></iframe>
</body>
....
inner.html
<head>
<script type="text/javascript">
var somevar = { id:302 };
(function() {
var ml = document.createElement('script');
ml.type = 'text/javascript';
ml.async = true;
ml.src = './some.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ml, s);
}) ();
</script>
</head>
some.js:
console.log(parent.somevar);
我期望的是some.js中的代碼將輸出“ somevar”對象,但輸出“未定義”。 如果我直接運行inner.html(而不是通過iframe元素),則它會很好地輸出對象。
如果我做
console.log(parent);
我得到了父窗口的DOM(outer.html)。
所以我的問題是-我如何從some.js文件(或inner.html中的其他腳本標簽)獲取inner.html的DOM
BR / Sune
console.log(somevar)
嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.