简体   繁体   English

在 HTML/JavaScript 中嵌入动态页面

[英]Dynamic page embedding in HTML/JavaScript

I have a tag like this in my HTML page:我的 HTML 页面中有这样的标签:

<embed src="http://..." style='...'>

Using Python-Flask I hand over a varying source address and store it in window.dynamicEmbedding .使用 Python-Flask,我交出一个不同的源地址并将其存储在window.dynamicEmbedding中。 Now I want to change the src value dynamically on page load in a compact way.现在我想以紧凑的方式在页面加载时动态更改 src 值。 I imagine something this我想象这个

<embed src=<script>document.write(window.dynamicEmbedding)</script> style='...'>

which is surely not possible.这肯定是不可能的。 Is there anyway to realize this without a lot of coding?无论如何,无需大量编码即可实现这一点?

It would depend on the logic you want for deciding which urls to serve.这将取决于您决定要提供哪些网址的逻辑。 If you simply want a random url, that would be fairly easy in javascript.如果您只是想要一个随机的 url,那么在 javascript 中这将相当容易。 Simply place the urls in an array and use math.Random to select an index.只需将 url 放在一个数组中并使用 math.Random 到 select 一个索引。 Each time the page loads, a new random index can be generated, and a new url is selected to be passed to your embed.每次页面加载时,都会生成一个新的随机索引,并选择一个新的 url 传递给您的嵌入。

Since you're using flask though, the easier way - particularly if you need more complex selection criteria than random - would be to write the selection logic into the python, and pass only the selected url to the template.由于您使用的是 flask,因此更简单的方法 - 特别是如果您需要比随机更复杂的选择标准 - 将选择逻辑写入 python,并仅将选定的 Z572D4E421E5E6B7111D815E8A02 传递给模板。 You have a flask view like this:你有一个像这样的 flask 视图:

import random

@app.route('/')
def video_page():
    urls = ['1','2','3','4','5','6','etc']
    url_selection = random.choice(urls)
    return render_template('video_page.html', url=url_selection)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM