我可以用什么来将YouTube视频的链接存储到我的页面中?

时间:2022-05-17 09:14:19

I want to create a video slideshow from scratch for my webpage.

我想从头开始为我的网页创建一个视频幻灯片。

Instead of displaying the videos directly I've managed so far to write a code using JavaScript which takes a YouTube video URL and takes its thumbnail to show it on the slideshow and here goes the problem:

我没有直接显示视频,而是使用JavaScript编写了一个代码,该代码使用了一个YouTube视频网址并将其缩略图显示在幻灯片上,这就解决了问题:

The URL from the video has to be stored somewhere so the script can work properly, I've been hiding the URL inside the HTML code using the "src" attribute but that means I'll have to change it directly in the HTML code every time I want to display a different video on the slideshow.

视频中的URL必须存储在某处,以便脚本可以正常工作,我一直使用“src”属性将URL隐藏在HTML代码中,但这意味着我必须在HTML代码中直接更改它我希望在幻灯片上显示不同的视频。

I was thinking using a database (SQL) to store the URLs, but I read that linking JS/HTML and SQL is a bad practice and brings up unnecessary issues and will bring a headache and not a solution. So where or what can I use to store those URLs and link them properly into my code?

我正在考虑使用数据库(SQL)来存储URL,但我读到链接JS / HTML和SQL是一种不好的做法,会带来不必要的问题,并且会带来头疼而不是解决方案。那么我可以使用哪些地方或什么来存储这些URL并将它们正确地链接到我的代码中?

我可以用什么来将YouTube视频的链接存储到我的页面中?

1 个解决方案

#1


1  

A database is the right answer, however, for just links, you may not want SQL. I would suggest JSON, but that may not even be necessary here. You can load an array of links from an external JS file. The contents of it would be:

数据库是正确的答案,但是,对于只是链接,您可能不需要SQL。我会建议使用JSON,但这可能不是必需的。您可以从外部JS文件加载一系列链接。它的内容是:

var youtubeLinks = [
  'https://youtube.com/link1',
  'https://youtube.com/link2',
  'https://youtube.com/link3'
];

If that file was called script.js and it was stored in your website root, then you can load it into your HTML as such:

如果该文件名为script.js并且存储在您的网站根目录中,那么您可以将其加载到HTML中:

<script src="script.js"></script>

Then in your javascript code that you already wrote, you can pass each link through a loop. Maybe something like this:

然后在您已编写的javascript代码中,您可以通过循环传递每个链接。也许是这样的:

for (var i = 0; i < youtubeLinks.length; i++) {
  thisLink = youtubeLinks[i];
  // your code here
}

#1


1  

A database is the right answer, however, for just links, you may not want SQL. I would suggest JSON, but that may not even be necessary here. You can load an array of links from an external JS file. The contents of it would be:

数据库是正确的答案,但是,对于只是链接,您可能不需要SQL。我会建议使用JSON,但这可能不是必需的。您可以从外部JS文件加载一系列链接。它的内容是:

var youtubeLinks = [
  'https://youtube.com/link1',
  'https://youtube.com/link2',
  'https://youtube.com/link3'
];

If that file was called script.js and it was stored in your website root, then you can load it into your HTML as such:

如果该文件名为script.js并且存储在您的网站根目录中,那么您可以将其加载到HTML中:

<script src="script.js"></script>

Then in your javascript code that you already wrote, you can pass each link through a loop. Maybe something like this:

然后在您已编写的javascript代码中,您可以通过循环传递每个链接。也许是这样的:

for (var i = 0; i < youtubeLinks.length; i++) {
  thisLink = youtubeLinks[i];
  // your code here
}