如何确定iframe的位置是否已更改? [英] How to determine if the location of the iframe has changed?

查看:200
本文介绍了如何确定iframe的位置是否已更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iframe,它显示来自外部网站的表单,一旦表单被提交,它就被重定向到另一个有一条感谢信息的页面。如果src网页的位置有,我们可以从iframe知道改变了吗?我希望我能有所作为......

I have got an iframe that displays a form from an external site,once the form is submitted it is redirected to another page that has got a thankyou message.Is it posiible to know from the iframe if the location of src webpage has changed? i hope im making some sense...

我得到了这个代码,它可以从我的网站上找到我的工作,它在我的网站上工作,但当我尝试在本地做到这一点我得到访问被拒绝javascript错误....

i got this code which will do the job for me from some website,its working while im in their website but when i try to do it locally im getting Access Denied javascript error....

<html>
<body>
<script language="JavaScript">
    function myLocation() {
        alert(document.all.myFrame.contentWindow.location);
    }
</script>
<iframe id="myFrame" src="http://www.java2s.com" style="width:200;">
</iframe>
<br>
<button onclick="myLocation();">Location of Frame</button>
</body>
</html>

谢谢

推荐答案

您可能想要使用 onLoad 事件,如下例所示:

You may want to use the onLoad event, as in the following example:

<iframe src="http://www.google.com/" onLoad="alert('Test');"></iframe>

只要iframe中的位置发生变化,警报就会弹出。这种技术的唯一问题是它可能无法与IE5和早期Opera等旧版浏览器一起使用。 (来源

The alert will pop-up whenever the location within the iframe has changed. The only problem with this technique is that it may not work with some older browsers like IE5 and early Opera. (Source)

更新:

进一步编辑,您收到拒绝访问错误因为您无法访问不在父窗口的同一域内的网站的 contentWindow.location 。虽然这对您的真实要求是不幸的,但这被视为一项安全限制,以防止交叉网站脚本

Further to your edit, you are getting the "Access Denied" error because you cannot access the contentWindow.location of a website that is not within the same domain of the parent window. While this is unfortunate for your genuine requirement, this is considered a security restriction to prevent cross-site scripting.

这篇关于如何确定iframe的位置是否已更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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