【发布时间】:2017-10-29 19:58:35
【问题描述】:
有没有办法捕获对iframe 内链接的点击?
<iframe *ngIf="currentFrameUrl" id="contentFrame" [src]="currentFrameUrl | safe"></iframe>
我的 iframe 组件只包含一个订阅可观察变量的简单代码。
export class ContentFrameComponent implements OnInit {
currentFrameUrl: string;
currentMenuState: boolean;
@ViewChild('contentFrame') iframe: ElementRef;
constructor(private frameService: ContentFrameService) {
this.currentMenuState = true;
}
ngOnInit(): void {
this.frameService.providerCurrentUrl$.subscribe(data => this.currentFrameUrl = data);
this.frameService.providerMenuState$.subscribe(data => this.currentMenuState = data);
}
ngAfterViewInit() {
let doc = this.iframe.nativeElement.contentDocument ||this.iframe.nativeElement.contentWindow;
if (typeof doc.addEventListener !== undefined) {
console.log("inside if - addEventListener") // Is shown
doc.addEventListener("click", this.iframeClickHandler, false)
} else if (typeof doc.attachEvent !== undefined) {
console.log("inside if - attachEvent ") // Does not show
doc.attachEvent("onclick", this.iframeClickHandler)
}
}
iframeClickHandler(): void {
console.log("Click test") // Does not show
}
}
我的目标是捕捉iframe 链接上的点击事件,停止其传播并使用router.navigate 或location.go/location.replaceState 设置iframe 链接的url。还不确定哪个选项更好。
我无法控制iframe 中加载的页面。我只知道,链接应该包含可以与我的动态路由一起使用的url。
【问题讨论】:
-
iframe 是否与主机页面来自同一个域?
-
是的,我正在处理 Intranet 应用程序的覆盖。
标签: html angular typescript iframe