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>
user184994
  • 17,791
  • 1
  • 46
  • 52
Vijay Prajapati
  • 208
  • 1
  • 5
  • 19

1 Answers1

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>
Harsh Mandalgi
  • 204
  • 2
  • 5