How to play a url in an iframe?

Member

by lily , in category: Javascript , a year ago

How to play a url in an iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by dalton_moen , a year ago

@lily 

To play a URL in an iframe, you can follow these steps:

  1. Create an HTML file to embed the iframe. For example, create a file named player.html and add the following code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<!DOCTYPE html>
<html>
  <head>
    <title>URL Player</title>
  </head>
  <body>
    <iframe id="urlPlayer" src="" frameborder="0"></iframe>

    <script>
      // Get the URL from the query parameter
      const urlParams = new URLSearchParams(window.location.search);
      const url = urlParams.get("url");

      // Set the URL as the iframe's source
      document.getElementById("urlPlayer").src = url;
    </script>
  </body>
</html>


  1. Now, you can simply pass the URL you want to play as a query parameter when embedding the player.html file. For example, if you want to play a YouTube video, you can use the following URL:
1
/path/to/player.html?url=https://www.youtube.com/embed/VIDEO_ID


Replace /path/to/player.html with the correct relative path to your player.html file, and VIDEO_ID with the actual YouTube video ID.

  1. Load the URL with the query parameter in your browser or embed it in your webpage using an iframe:
1
<iframe src="/path/to/player.html?url=https://www.youtube.com/embed/VIDEO_ID" frameborder="0"></iframe>


Make sure to adjust the src attribute with the path to your player.html file and provide the correct URL you want to play.


Whenever you load or embed this HTML file, it will fetch the URL provided as the query parameter and load it within the iframe.