1

Here is the content that showing, i am trying to display the youtube content on my website but its not showing

Here the html that displaying on the browser

<iframe width="480" height="270" src="https://www.youtube.com/embed/HK6B2da3DPA?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen>

Im using the following to bind that value:

<div class="text-center"> {{articleDatils.raw_content_path}} </div>
8
  • How are you adding the iframe to the template? Commented Jul 15, 2018 at 18:53
  • <div class="text-center"> {{articleDatils.raw_content_path}} </div> Commented Jul 15, 2018 at 18:54
  • using only this for displaying Commented Jul 15, 2018 at 18:55
  • 1
    You'll need to bind to the innerHTML property. Take a look at stackoverflow.com/questions/34585453/… Commented Jul 15, 2018 at 18:56
  • 2
    You need to convert it to safe HTML as well. stackblitz.com/edit/… Commented Jul 15, 2018 at 19:04

1 Answer 1

3

You need to sanitize the URL before using it in iframe. This need to be done beacause it helps to prevent Cross Site Scripting, i.e. it prevents attackers from injecting malicious client-side scripts into web pages, which is often referred to as Cross-site Scripting or XSS.

This method worked for me:

Import this header file:

import { DomSanitizer } from '@angular/platform-browser';

I created a function as:

getUrl(post)
{
  return this.sanitizer.bypassSecurityTrustResourceUrl('https://'+ post.url +'/');
}

Then in the template:

<div  class="iframe-container d-none d-lg-block">
   <iframe [src]='getUrl(post)'>
      <p>Your browser does not support iframes.</p>
   </iframe> 
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

You will need sanitizer: DomSanitizer; in your class definition

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.