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

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

问题描述

部分代码:



我的下面的代码从我的数据库中提取查询,然后使用inner.HTML =来显示div中的数据。它在原始使用中工作正常....



然而,在iFrame中调用以下版本,因为它用于更新页面。



页面没有错误,然后JavaScript会触发,但最后一行不起作用...



我刚刚意识到这可能是因为它正在加载隐藏的iFrame,它试图在iFrame中设置div的innerHTML,当然不会有效。



这是怎么回事?它没有意义,因为我有另一个脚本以相同的方式调用JavaScript,并且工作正常。

 <?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> [消息 '] '< $行。'; BR>';
$ p = $ p。'< a href =http://www.sharingizcaring.com/bleepV2/'.$row[username]。'>'。$ row [username]。' < / A> 。$行[日期]。< / DIV>;
$ p = $ p。'< div style =clear:both;>< / div>';
$ p = $ p。'< div id =dotted-line>< / div>';
}


$ p = addslashes($ p);
?>

< script>

alert('帖子正在解雇?');

document.getElementById('posts')。innerHTML ='为什么这会导致失败?';
< / script>


解决方案

你可以做到! 请阅读此处以获取更多信息



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

  //通常你使用... 
var whatever =value;

//使用窗口对象,在iframe和父
window.whatever =value之间共享
//;

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

...

  parent.someattr; 

//或试试这个
parent.getElementById('some_element');


Partial Code:

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....

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

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

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.

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>

解决方案

You can do it! Read here for more info

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

inside the iframe you can use...

parent.someattr;

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

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

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