question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Binding to the src of an iframe causes the iframe to flicker

See original GitHub issue

Binding to the src property of an iframe causes the iframe to reload every time change detection is run, even when the src value has not changed.

Reproduction: https://plnkr.co/edit/CgLHkprl4jYndqEiY7mX?p=preview (enter X1RVYt2QKQE as an example videoId, then type into the unrelated input)

Happens with all Angular versions <= 4.1 (current version at time of this writing)

Note from @tbosch:

So the problem is that this.domSanitizer.bypassSecurityTrustResourceUrl returns a new object every time it is invoked. If the result is cached, the flickering is gone, see https://plnkr.co/edit/W0gJKmRsGUpzb1QNZqf5?p=preview

Eventually, we could change our behavior in that bypassSecurityTrustResourceUrl would use an internal cache already.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:20
  • Comments:10

github_iconTop GitHub Comments

38reactions
juanbenitoprcommented, Mar 8, 2018

@anupvarma @danieltorrer I have found a solution, you just have to put the email modifying the dom by your own. I attach a code sample below

component.html <iframe src="" frameborder="0" height="700" ></iframe>

component.ts

 constructor(
    private hostElement: ElementRef,
  ) { }

ngOnInit(){
 const iframe = this.hostElement.nativeElement.querySelector('iframe');
 iframe.src = this.url;
}
24reactions
danieltorrercommented, Mar 8, 2018

Thanks @juanbenitopr For me the solution was to save the safeUrl and use it on [src] but setting it directly to the dom seems also like a great solution

<iframe [src]="video.safeURL" frameborder="0" height="700" ></iframe>

this.video.safeURL = this.sanitizer.bypassSecurityTrustResourceUrl( url );
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set <iframe src="..."> without causing `unsafe value ...
Error: unsafe value used in a resource URL context at DomSanitizationServiceImpl.sanitize... I have already tried using bindings with [src] with ...
Read more >
The ultimate guide to iframes - LogRocket Blog
Not a fan of iframes? This post provides an overview of the tag's best features, shows you how to use them, and how...
Read more >
Prevent White Flash While Iframe Loads - CSS-Tricks
It inserts some CSS on the page (right away) which makes all iframes invisible. You can't see a white flash on an iframe...
Read more >
chrome iframe flicker - CodePen
Insecure Resource. The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https....
Read more >
iFrame Resizer - GitHub Pages
The most likely cause of this problem is having set the height of an element to be 100% of the page somewhere in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found