简体   繁体   English

将特定页面加载到iframe中时如何调用JavaScript

[英]How to call javascript when specific page is loaded into an iframe

I think this would be pretty easy. 我认为这很容易。

I have a page that loads and iframe. 我有一个页面加载和iframe。 The iframe is another site I am working with. iframe是我正在使用的另一个网站。 I would like to have some javascript executed, on the parent page, if there is a specific page loaded in the iframe. 如果iframe中加载了特定的页面,我希望在父页面上执行一些javascript。

Does that make sense? 那有意义吗?

For example: foo.com is my main page with foobar.com loading in an iframe. 例如:foo.com是我的主页,在iframe中加载了foobar.com。 If user goes to foobar.com/thanks.html I want the script on foo.com to execute. 如果用户转到foobar.com/thanks.html,我希望foo.com上的脚本执行。

Appreciate any help. 感谢任何帮助。

As for detecting when the iframe has redirected this is very simple, you can use the onload attribute on the iframe element to trigger a function, and it will trigger every time the user redirects. 至于检测何时将iframe重定向,这非常简单,您可以使用iframe元素上的onload属性来触发功能,并且它将在用户每次重定向时触发。 However when trying to access the new url once redircted is not possible due to cross site scripting security issues. 但是,由于跨站点脚本安全性问题,当尝试访问新的URL时,一旦重新定义就不可能。

you mention however the iframe site is a site you are working with, since that is the case, what you are after may be possible if you have access to the .htaccess file 您提到的是iframe网站,但是您正在使用的网站是这种情况,因此,如果您可以访问.htaccess文件,则可能会发生此事

add

Header add Access-Control-Allow-Origin: http://parentdomain.com

and then on the parent domain 然后在父域上

include this html: 包括这个HTML:

<iframe src="http://childdomain" id="childsite" onload="iframeredirected();"/>

and this javascript 和这个JavaScript

function iframeredirected(){
var new_src = document.getElementById("childsite").documentWindow.location.href;
}

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

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