How to Dynamically Generate an Embedded YouTube Video from a URL Input Without Reloading the Page

0 votes
Can you tell me How to Dynamically Generate an Embedded YouTube Video from a URL Input Without Reloading the Page?
Mar 11 in Java-Script by Ashutosh
• 23,230 points
53 views

1 answer to this question.

0 votes

You can use JavaScript to extract the video ID from the URL and then insert the corresponding YouTube iframe embed code into the DOM. 

Steps:

1. HTML Structure

<input type="text" id="youtube-url" placeholder="Enter YouTube URL">

<button id="load-video">Load Video</button>

<div id="video-container"></div>

2. JavaScript to Handle URL Input and Embed Video

document.getElementById('load-video').addEventListener('click', function () {

  const youtubeUrl = document.getElementById('youtube-url').value;

  const videoId = extractVideoId(youtubeUrl);

  if (videoId) {

    const embedCode = `<iframe width="560" height="315" src="https://www.youtube.com/embed/${videoId}" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>`;

    document.getElementById('video-container').innerHTML = embedCode;

  } else {

    alert('Invalid YouTube URL');

  }

});

function extractVideoId(url) {

  // Regex to extract video ID from various YouTube URL formats

  const regex = /(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|(?:youtu\.be\/)([a-zA-Z0-9_-]{11})/;

  const match = url.match(regex);

  return match ? match[1] : null;

}

answered Mar 11 by Tanvi

Related Questions In Java-Script

0 votes
1 answer

How do I modify the URL without reloading the page?

Hii @kartik, HTML5 introduced the history.pushState() and history.replaceState() methods, which allow you ...READ MORE

answered Jun 20, 2020 in Java-Script by Niroj
• 82,840 points
5,726 views
0 votes
1 answer

How to show a different value from an input that what will be received as php?

Hello @kartik, You can't change the field's value ...READ MORE

answered Jul 8, 2020 in Java-Script by Niroj
• 82,840 points
11,661 views
0 votes
1 answer

How to get the host url using javascript from the current page?

Hello @kartik, Use: var host = window.location.hostname; or possibly var host ...READ MORE

answered Oct 9, 2020 in Java-Script by Niroj
• 82,840 points
1,494 views
0 votes
1 answer

How can we change the page URL without refreshing the page?

Hello @kartik,  In HTML5 you can change the ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,840 points
1,755 views
0 votes
1 answer

How to Center a Single-Column Div Within a 12-Column Container in Bootstrap 3?

Using Bootstrap's Offset Classes Bootstrap 3 provides offset ...READ MORE

answered Mar 11 in Node-js by Tanvi
66 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How do I delete a specific element from an array in JavaScript?

You can use the splice() method. Code: let arr ...READ MORE

answered Jan 10 in Java-Script by Navya
123 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP