我正在Angular网站上工作
我通过iframe打开另一个网站,
如何阅读iframe网站的网址?
我正在把我的代码
.html文件
<iframe src="https://www.zaubacorp.com/">
<p>Your browser does not support iframes.</p>
</iframe>
当我搜索时如何阅读Angular中iframe网站的网址。
我在javascript How do I get the current location of an iframe?中得到以下建议
alert(document.getElementById("myiframe").src);
document.getElementById("myiframe").src = 'http://www.google.com/';
alert(document.getElementById("myiframe").documentWindow.location.href);
Error: Permission denied to get property Location.href
但实际上它不适用于Angular
通过ViewChild,您可以访问iframe元素并读取src。
import { Component, ViewChild, ElementRef } from '@angular/core';
@Component({
selector: 'hello',
templateUrl: 'hello.component.html',
styles: [`h1 { font-family: Lato; }`]
})
export class HelloComponent {
@ViewChild('iframe') iframe: ElementRef;
ngAfterViewInit() {
console.log(this.iframe.nativeElement.src);
}
}
你的HTML:
<iframe #iframe src="https://www.zaubacorp.com/">
<p>Your browser does not support iframes.</p>
</iframe>
对于演示检查这个this stackblitz