在 iframe 中运行的 javascript 会影响主页吗? [英] Can javascript running inside an iframe affect the main page?

查看:25
本文介绍了在 iframe 中运行的 javascript 会影响主页吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

部分代码:

我下面的代码从我的数据库中提取一个查询,然后使用 inner.HTML = 显示一个 div 中的数据.它在原始使用中运行良好....

My below code pulls a query from my DB and then uses inner.HTML = to display the data inside a div. It works fine in it original use....

但是,下面的版本在 iFrame 中被调用,因为它用于更新页面.

However the below version is called inside a iFrame as it is used to update the page.

页面没有错误并且 JavaScript 触发但是最后一行不起作用...

The page has no errors and the JavaScript fires however the last line does not work...

我刚刚意识到,也许因为它正在隐藏的 iFrame 中加载,它试图在 iFrame 内设置 div 的 innerHTML,这当然不起作用.

I have just realized that perhaps since it is loading in the hidden iFrame it is trying to set the innerHTML of a div inside the iFrame and that of course will not work.

这是怎么回事?这是没有意义的,因为我有另一个脚本,它在它的末尾以相同的方式调用 JavaScript,并且运行良好.

Is this what is happening? It doesn't make sense because I have another script that calls JavaScript at the end of it in the same manner and it works fine.

<?php
    while ($row = mysql_fetch_array($result))
    {
        $p = $p.'<div id="left"><img src="http://www.sharingizcaring.com/bleepV2/images/thumbs/'.$row[image].'" /></div>';
        $p = $p.'<div id="right"><h2>'.$row[artist].' - '.$row['title'].'</h2><br>'.$row['message'].'<br>';
        $p = $p.'<a href="http://www.sharingizcaring.com/bleepV2/'.$row[username].'">'.$row[username].'</a> '.$row[date].'</div>';
        $p = $p.'<div style="clear: both;"></div>';
        $p = $p.'<div id="dotted-line"></div>';
    }


    $p = addslashes($p);
?>

<script>

        alert('posts are firing? ');

        document.getElementById('posts').innerHTML = 'why doth this faileth?';
</script>

推荐答案

你可以做到!阅读此处了解更多信息

您可以通过设置和从 window 元素获取变量来影响包含 iframe 的文档:

You can affect the document with contains the iframe by setting and getting variables from the window element:

// normally you use...
var whatever = "value";

// use the window object instead, which is shared 
// between the iframe and the parent
window.whatever = "value";

你应该知道的另一件事是你可以通过父对象访问主文档

The other thing you should know is that you can access the main document via the parent object

您可以在 iframe 内使用...

inside the iframe you can use...

parent.someattr;

// or try this
parent.getElementById('some_element');

这篇关于在 iframe 中运行的 javascript 会影响主页吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆