简体   繁体   English

在iframe(不是父页面)中从DOM获取价值

[英]Get value from DOM in iframe (not parent page)

I know the subject might be a bit confusing, but in my searching i found LOTS of questions about getting value from parent page when inside iframe. 我知道这个主题可能有点混乱,但是在我的搜索中,我发现很多关于在iframe中从父页面获取价值的问题。 This is not what i want to achive. 这不是我想要达到的目标。

I have 2 html pages and a js file. 我有2个html页面和一个js文件。 This is how they look: 这是它们的外观:

outer.html: external.html:

...
<body>
<iframe src="./inner.html"></iframe>
</body>
....

inner.html 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: some.js:

console.log(parent.somevar);

What im expecting is that code in some.js would output the "somevar" object, but it outputs 'undefined'. 我期望的是some.js中的代码将输出“ somevar”对象,但输出“未定义”。 If i run the inner.html directly (not through iframe element) then it outputs the object fine. 如果我直接运行inner.html(而不是通过iframe元素),则它会很好地输出对象。

If i do 如果我做

console.log(parent);

I get the DOM of the parent window (outer.html). 我得到了父窗口的DOM(outer.html)。

So my question is - how do i get the DOM of inner.html from the some.js file (or other script tags in the inner.html) 所以我的问题是-我如何从some.js文件(或inner.html中的其他脚本标签)获取inner.html的DOM

BR/Sune BR / Sune

console.log(somevar)吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM