iframe onload事件 [英] iframe onload event

查看:116
本文介绍了iframe onload事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段简单的代码:

 < script> 
function change(){document.getElementById(browse)。src = document.getElementById(addr)。value;}
function update(){document.getElementById(addr)。 value = document.getElementById(browse)。src;}
< script>
< input type =textid =addr>< input type =buttonvalue =Goonclick =change();>
< iframe id =browsestyle =width:100%; height:100%onload =update();>< / iframe>

update();在例如没有被调用时链接里面的iframe被点击并加载新的页面。



有什么问题?

解决方案

简短回答:您无法获取除您以外的任何域的网页网址。您需要的URL可以通过以下方式获得:

  document.getElementById(browse)。contentWindow.location.href; 

然而,只有浏览 域中的网站时才能使用此网址由于同源策略。这将是未定义的,如果您尝试在任何其他域上,您将收到安全错误。



因此,如果您想从其他域获取页面并且想要执行此操作这是纯JavaScript,你不能。



长答案:您可以使用服务器端代理来加载请求的网址iframe。例如:



http://yourdomain.com/proxy.php?url=http://www.google.com/

然后当获取URL获取url参数并将其放入'addr'输入中。



这种方法实际上并不值得。您将使用大量的带宽,并且您将打开您的网站,让希望通过防火墙进行代理并滥用代理服务以满足其需求的用户。

I've a simple piece of code:

<script>
function change(){document.getElementById("browse").src = document.getElementById("addr").value;}
function update(){document.getElementById("addr").value = document.getElementById("browse").src;}
<script>
<input type="text" id="addr"><input type="button" value="Go" onclick="change();">
<iframe id="browse" style="width:100%;height:100%" onload="update();"></iframe>

update(); is not called when e.g. link inside the iframe was clicked and new page loaded.

What's the problem?

解决方案

Short answer: You can't get the URL of pages on any domain other than yours. The URL you want can be gotten with:

document.getElementById("browse").contentWindow.location.href;

However, this URL will only be available when browsing sites on your domain due to the Same origin policy. It will be undefined and you will get a security error if you try on any other domain.

So, if you want to get pages from other domains and you want to do this with pure javascript, you can't.

Long answer: You could use a server-side proxy to load in the URLs requested in the iframe. Something like:

http://yourdomain.com/proxy.php?url=http://www.google.com/

And then when getting the URL get the url parameter and put that in the 'addr' input.

This approach is really not worth doing; You will use a lot of bandwidth and you will open your site up to people wanting to proxy through a firewall and abusing your proxy for their needs.

这篇关于iframe onload事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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